Open In App

Python | Reversed Order keys in dictionary

Improve
Improve
Like Article
Like
Save
Share
Report

While working with dictionary, we can sometimes, have a problem in which we require to print dictionaries in the reversed order of their occurrence. Let’s discuss certain ways in which this problem can be solved. 

Method #1 : Using reversed() + sorted() + keys() + loop The combination of above functions can be used to perform this particular task. The sorted function is used to sort the keys and reversed gets the keys extracted using keys(), in descending order, which are printed using a loop. 

Python3




# Python3 code to demonstrate working of
# Reversed Order keys in dictionary
# Using sorted() + keys() + reversed() + loop
 
# initializing dictionary
test_dict = {1 : "Gfg", 5 : "is", 4 : "the", 2 : "best"}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# Using sorted() + keys() + reversed() + loop
# Reversed Order keys in dictionary
res = []
for ele in reversed(sorted(test_dict.keys())):
    res.append(ele)
 
# printing result
print("The reversed order of dictionary keys : " + str(res))


Output : 

The original dictionary is : {1: 'Gfg', 2: 'best', 4: 'the', 5: 'is'}
The reversed order of dictionary keys : [5, 4, 2, 1]

Time Complexity: O(N), where N is the length of the input string.

Auxiliary Space: O(N)

Method #2 : Using list() + keys() + sorted() + reversed() It is another method in which this task can be solved. This is just a small variation of the above method, in this the list function is used to convert the result to list rather than using the loop to print the variables. 

Python3




# Python3 code to demonstrate working of
# Reversed Order keys in dictionary
# Using sorted() + keys() + reversed() + list()
 
# initializing dictionary
test_dict = {1 : "Gfg", 5 : "is", 4 : "the", 2 : "best"}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# Using sorted() + keys() + reversed() + list()
# Reversed Order keys in dictionary
res = list(reversed(sorted(test_dict.keys())))
 
# printing result
print("The reversed order of dictionary keys : " + str(res))


Output : 

The original dictionary is : {1: 'Gfg', 2: 'best', 4: 'the', 5: 'is'}
The reversed order of dictionary keys : [5, 4, 2, 1]

Method #3 : Using list comprehension + keys() + sorted() + reversed()
It is also another way to perform this task, list comprehension is used to perform this task.

Python3




# Python3 code to demonstrate working of
# Reversed Order keys in dictionary
# Using sorted() + keys() + reversed() + list comprehension
   
# initializing dictionary
test_dict = {1 : "Gfg", 5 : "is", 4 : "the", 2 : "best"}
   
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
   
# Using sorted() + keys() + reversed() + list comprehension
# Reversed Order keys in dictionary
res = [ele for ele in reversed(sorted(test_dict.keys()))]
   
# printing result
print("The reversed order of dictionary keys : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy


Output

The original dictionary is : {1: 'Gfg', 5: 'is', 4: 'the', 2: 'best'}
The reversed order of dictionary keys : [5, 4, 2, 1]

Time complexity: O(nlogn)
Auxiliary space: O(n)

Method #4: Using the heapq module

We can use the heapq.nlargest() function to get the n largest keys from the dictionary, where n is the number of items in the dictionary.
We can set n to None to get all the keys, and use the reverse=True argument to sort them in reverse order.

Python3




# import heapq module
import heapq
 
# initializing dictionary
test_dict = {1: "Gfg", 5: "is", 4: "the", 2: "best"}
 
# printing original dictionary
print("The original dictionary is: " + str(test_dict))
 
# Using heapq.nlargest() to get the keys in ascending order
res = sorted(test_dict.keys(), reverse=True)
 
# printing result
print("The reversed order of dictionary keys: " + str(res))


Output

The original dictionary is: {1: 'Gfg', 5: 'is', 4: 'the', 2: 'best'}
The reversed order of dictionary keys: [5, 4, 2, 1]

Time complexity: O(nlogn) for sorting the dictionary keys using heapq.nlargest(), where n is the number of items in the dictionary.
Auxiliary space: O(n) for creating a list of keys.



Last Updated : 03 May, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads