Python | Reversed Order keys in dictionary
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 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)) |
The original dictionary is : {1: 'Gfg', 2: 'best', 4: 'the', 5: 'is'}
The reversed order of dictionary keys : [5, 4, 2, 1]
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 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)) |
The original dictionary is : {1: 'Gfg', 2: 'best', 4: 'the', 5: 'is'}
The reversed order of dictionary keys : [5, 4, 2, 1]
Recommended Posts:
- Python | Add new keys to a dictionary
- Python | Get all tuple keys from dictionary
- Python | Get dictionary keys as a list
- Python | Get the number of keys with given value N in dictionary
- Python | Grouping dictionary keys by value
- Python Dictionary | keys() method
- Python | Minimum value keys in Dictionary
- Python | Count keys with particular value in dictionary
- Python | Add keys to nested dictionary
- Python | Get total keys in dictionary
- Python | Remove spaces from dictionary keys
- Python dictionary with keys having multiple inputs
- Python | Sort dictionary keys to list
- Python | Initialize a dictionary with only keys from a list
- Python | Initialize dictionary with multiple keys
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



