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
# Python code to demonstrate # to get sorted items from dictionary # initialising _dictionary ini_dict = { 'a' : 'akshat' , 'b' : 'bhuvan' , 'c' : 'chandan' } # printing iniial_dictionary print ( "iniial_dictionary" , str (ini_dict)) # getting items in sorted order print ( "\nItems in sorted order" ) for key in sorted (ini_dict): print (ini_dict[key]) |
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
# Python code to demonstrate # to get sorted items from dictionary # initialising _dictionary ini_dict = { 'a' : 'akshat' , 'b' : 'bhuvan' , 'c' : 'chandan' } # printing iniial_dictionary print ( "iniial_dictionary" , str (ini_dict)) # getting items in sorted order print ( "\nItems in sorted order" ) for key, value in sorted (ini_dict.items()): print (value) |
iniial_dictionary {'a': 'akshat', 'b': 'bhuvan', 'c': 'chandan'} Items in sorted order akshat bhuvan chandan
Method #3: Using operator
Python
# Python code to demonstrate # to get sorted items from dictionary import operator # initialising _dictionary ini_dict = { 'a' : 'akshat' , 'b' : 'bhuvan' , 'c' : 'chandan' } # printing iniial_dictionary print "iniial_dictionary" , str (ini_dict) # getting items in sorted order print ( "\nItems in sorted order" ) for key, value in sorted (ini_dict.iteritems(), key = operator.itemgetter( 1 ), reverse = False ): print key, " " , value |
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
#initializing dictionary ini_dict = { 'a' : 'akshat' , 'b' : 'bhuvan' , 'c' : 'chandan' } #printing initial dictionary print ( "initial dictionary:" , ini_dict) # getting items in sorted order sorted_items = sorted (ini_dict.items(), key = lambda x: x[ 1 ]) #printing the sorted items print ( "items in sorted order:" , sorted_items) |
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
#importing operator module import operator #initializing dictionary ini_dict = { 'a' : 'akshat' , 'b' : 'bhuvan' , 'c' : 'chandan' } #printing initial dictionary print ( "initial dictionary:" , ini_dict) # getting items in sorted order using itemgetter sorted_items = sorted (ini_dict.items(), key = operator.itemgetter( 1 )) #printing the sorted items print ( "items in sorted order:" , sorted_items) |
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)