Reverse iterate through a Dictionary in Python

In this tutorial, we will see how to reverse iterate through a Dictionary in Python

To reverse a dictionary in Python, we can use reversed() method as follows.

d1 = {'Bob': 60, 'Alice': 43, 'Ram': 80, 'Geeta': 49, 'Shreyan': 95, 'Raj': 68}
print('Before reverse')
print(d1)
reversed_d1 = {key: value for key, value in reversed(d1.items())}
print('After reverse')
print(reversed_d1)
Reverse iterate through a Dictionary in Python

The output of the preceding code will be as follows.

Before reverse
{'Bob': 60, 'Alice': 43, 'Ram': 80, 'Geeta': 49, 'Shreyan': 95, 'Raj': 68}
After reverse
{'Raj': 68, 'Shreyan': 95, 'Geeta': 49, 'Ram': 80, 'Alice': 43, 'Bob': 60}
Output: Reverse iterate through a Dictionary in Python

The output shows, after reverse iterating through the items in a dictionary, prints the values from the last to first.