
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python - Get items in sorted order from given dictionary
The Python dictionary has key and value pairs. In some situation we will need the items of the dictionary to be sorted according to the keys. In this article we'll see the different ways to get a sorted output from the items in the dictionary.
Using Operator Module
The Operator module has itemgetter function which can take 0 as the index of input parameter for the keys of the dictionary. We apply the sorted function on top of itemgetter and get the sorted output.
Example
dict = {12 : 'Mon', 21 : 'Tue', 17: 'Wed'} import operator print("\nGiven dictionary", str(dict)) print ("sorted order from given dictionary") for k, n in sorted(dict.items(),key = operator.itemgetter(0),reverse = False): print(k, " ", n)
Output
Running the above code gives us the following result −
Given dictionary {12: 'Mon', 21: 'Tue', 17: 'Wed'} sorted order from given dictionary 12 Mon 17 Wed 21 Tue
Using the Sorted Method
The sorted method can be directly applied on a dictionary which sorts the keys of the dictionary.
Example
dict = {12 : 'Mon', 21 : 'Tue', 17: 'Wed'} #Using sorted() print ("Given dictionary", str(dict)) print ("sorted order from given dictionary") for k in sorted(dict): print (dict[k])
Output
Running the above code gives us the following result −
Given dictionary {12: 'Mon', 21: 'Tue', 17: 'Wed'} sorted order from given dictionary Mon Wed Tue
Using dict.items()
We can also apply the sorted method to dict.items. In this case both the keys and values can get printed.
Example
dict = {12 : 'Mon', 21 : 'Tue', 17: 'Wed'} #Using d.items() print("\nGiven dictionary", str(dict)) print ("sorted order from given dictionary") for k, i in sorted(dict.items()): print(k,i)
Output
Running the above code gives us the following result −
Given dictionary {12: 'Mon', 21: 'Tue', 17: 'Wed'} sorted order from given dictionary 12 Mon 17 Wed 21 Tue
- Related Articles
- Delete items from dictionary while iterating in Python
- Get key from value in Dictionary in Python
- How to get a value for a given key from a Python dictionary?
- Dictionary Methods in Python (cmp(), len(), items()…)
- Python Program to get the first given number of items from the array
- Python Program to get the last given number of items from the array
- To print all elements in sorted order from row and column wise sorted matrix in Python
- Append Dictionary Keys and Values (In order ) in dictionary using Python
- Python - Filter the negative values from given dictionary
- Program to find all prime factors of a given number in sorted order in Python
- Python - Inserting item in sorted list maintaining order
- Python Program to Multiply All the Items in a Dictionary
- Find common elements in three sorted arrays by dictionary intersection in Python
- How we can create a dictionary from a given tuple in Python?
- Python Program to get first and last element from a Dictionary
