VOOZH about

URL: https://www.geeksforgeeks.org/python/python-reverse-dictionary-keys-order/

⇱ Reverse Dictionary Keys Order - Python - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Reverse Dictionary Keys Order - Python

Last Updated : 12 Jul, 2025

We are given a dictionary and our task is to reverse the order of its keys. This means if we have a dictionary like {'a': 1, 'b': 2, 'c': 3} then the output will be {'c': 3, 'b': 2, 'a': 1}. This can be done using methods like reversed(), dictionary comprehensions, or OrderedDict. Let's explore these methods below.

Using OrderedDict() + reversed() + items()

This method is for older versions of Python. Older versions don't keep order in dictionaries, hence have to converted to OrderedDict to execute this task. 


Output
{'gfg': 4, 'is': 2, 'best': 5}
OrderedDict({'best': 5, 'is': 2, 'gfg': 4})

Explanation: This method uses OrderedDict from the collections module to maintain the order of the dictionary while reversing the keys. It first converts the dictionary's items to a list using items(), then applies reversed() to reverse the order. Finally, it reconstructs the dictionary as an OrderedDict, which preserves the reversed key order.

Using reversed() + items()

The combination of reversed() and items() functions can be used to solve this problem. This is for newer versions of Python, which have dictionary in incoming order of elements. 


Output
{'gfg': 4, 'is': 2, 'best': 5}
{'best': 5, 'is': 2, 'gfg': 4}

Explanation: In this approach, the dictionary is reversed using reversed() on the items list, and the result is converted back to a standard dictionary using dict(). This method does not preserve order like OrderedDict but simply reverses the key-value pairs.

Using a loop and pop() to remove and re-add key-value pairs

One way to reverse the order of dictionary keys in Python is by using a loop and the popitem() method. This method removes key-value pairs from the original dictionary and re-adds them in reverse order to a new dictionary.


Output
{'gfg': 4, 'is': 2, 'best': 5}
{'best': 5, 'is': 2, 'gfg': 4}

Explanation: This approach manually removes and re-adds key-value pairs from the original dictionary using popitem(). By popping items from the end of the dictionary, the loop constructs a new dictionary with keys in reverse order. This method directly manipulates the dictionary without additional modules.

Using the sorted() function and a lambda function

We can use the sorted() function to sort the dictionary keys in reverse order, based on their original position in the list of keys (list(d.keys()).index(x)). Use a dictionary comprehension to create a new dictionary with the sorted keys and original values.


Output
{'gfg': 4, 'is': 2, 'best': 5}
{'best': 5, 'is': 2, 'gfg': 4}

Explanation: This method reverses the dictionary by sorting its keys in reverse order using the sorted() function with a lambda expression. It then constructs a new dictionary by iterating over the sorted keys and maintaining the original key-value pairs. This method provides a simple, flexible way to reverse key order.

Using deque

We use the deque data structure from the collections module to reverse the order of the dictionary keys. We then create a new dictionary object using a dictionary comprehension and the reversed keys and original values. Finally, we use the dict() constructor to convert the new dictionary to an OrderedDict object.


Output
OrderedDict([('best', 5), ('gfg', 4), ('is', 2)])

Explanation: This method uses deque from the collections module to reverse the dictionary's keys. It converts the keys into a deque, applies reverse(), and then creates a new dictionary using the reversed keys. The result is then wrapped into an OrderedDict to preserve the key order.

Comment