Python | Get items in sorted order from given dictionary
Last Updated :
23 Apr, 2023
Given a dictionary, the task is to get all items from the dictionary in sorted order. Let’s discuss different ways we can do this task.
Method #1: Using sorted()
Python3
ini_dict = { 'a' : 'akshat' , 'b' : 'bhuvan' , 'c' : 'chandan' }
print ( "iniial_dictionary" , str (ini_dict))
print ( "\nItems in sorted order" )
for key in sorted (ini_dict):
print (ini_dict[key])
|
Output:
iniial_dictionary {'b': 'bhuvan', 'c': 'chandan', 'a': 'akshat'}
Items in sorted order
akshat
bhuvan
chandan
Time Complexity: O(n*logn), as sorted() function is used.
Auxiliary Space: O(n), where n is length of the ini_dict.
Method #2: Using d.items()
Python3
ini_dict = { 'a' : 'akshat' , 'b' : 'bhuvan' , 'c' : 'chandan' }
print ( "iniial_dictionary" , str (ini_dict))
print ( "\nItems in sorted order" )
for key, value in sorted (ini_dict.items()):
print (value)
|
Output:
iniial_dictionary {'a': 'akshat', 'b': 'bhuvan', 'c': 'chandan'}
Items in sorted order
akshat
bhuvan
chandan
Method #3: Using operator
Python
import operator
ini_dict = { 'a' : 'akshat' , 'b' : 'bhuvan' , 'c' : 'chandan' }
print "iniial_dictionary" , str (ini_dict)
print ( "\nItems in sorted order" )
for key, value in sorted (ini_dict.iteritems(),
key = operator.itemgetter( 1 ),
reverse = False ):
print key, " " , value
|
Output:
iniial_dictionary {'a': 'akshat', 'c': 'chandan', 'b': 'bhuvan'}
Items in sorted order
a akshat
b bhuvan
c chandan
Method #4: Using sorted function along with a lambda function
Another approach to get the items in sorted order from a dictionary is to use the sorted function along with a lambda function as the key. The lambda function can extract the value from the tuple returned by the items method and return it. Then, the sorted function will use these values to sort the items in the dictionary.
Here is an example:
Python3
ini_dict = { 'a' : 'akshat' , 'b' : 'bhuvan' , 'c' : 'chandan' }
print ( "initial dictionary:" , ini_dict)
sorted_items = sorted (ini_dict.items(), key = lambda x: x[ 1 ])
print ( "items in sorted order:" , sorted_items)
|
Output
initial dictionary: {'a': 'akshat', 'b': 'bhuvan', 'c': 'chandan'}
items in sorted order: [('a', 'akshat'), ('b', 'bhuvan'), ('c', 'chandan')]
Time complexity: O(nlogn)
Auxiliary Space: O(1)
Method 5: Using the itemgetter function from the operator module
Here are the steps to implement this method:
- Import the operator module
- Initialize the dictionary ini_dict
- Use the itemgetter function with the sorted function to get the items in sorted order
- Print the sorted items
Python3
import operator
ini_dict = { 'a' : 'akshat' , 'b' : 'bhuvan' , 'c' : 'chandan' }
print ( "initial dictionary:" , ini_dict)
sorted_items = sorted (ini_dict.items(), key = operator.itemgetter( 1 ))
print ( "items in sorted order:" , sorted_items)
|
Output
initial dictionary: {'a': 'akshat', 'b': 'bhuvan', 'c': 'chandan'}
items in sorted order: [('a', 'akshat'), ('b', 'bhuvan'), ('c', 'chandan')]
Time complexity: O(n log n) where n is the number of items in the dictionary
Auxiliary space: O(n) where n is the number of items in the dictionary (for the sorted list of items)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...