Iterate over a dictionary in sorted order in Python
This post will discuss how to iterate over a dictionary in sorted order of its keys or values in Python.
With Python 3.7, a dictionary is guaranteed to be iterated in the insertion order of keys. If you need to iterate over a dictionary in sorted order of its keys or values, you can pass the dictionary’s entries to the sorted() function, which returns a list of tuples. You can get the view on the dictionary’s entries using either dict.items() or dict.keys() function.
1. Using dict.items() function
A standard solution to iterate over a dictionary in sorted order of keys is using the dict.items() with sorted() function.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
if __name__ == '__main__': d = {'one': 1, 'two': 2, 'three': 3, 'four': 4} for k, v in sorted(d.items()): print((k, v)) ''' Output: ('four', 4) ('one', 1) ('three', 3) ('two', 2) ''' |
To iterate in reverse order of keys, you can specify the reverse argument of the sorted() function as True.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
if __name__ == '__main__': d = {'one': 1, 'two': 2, 'three': 3, 'four': 4} for k, v in sorted(d.items(), reverse=True): print((k, v)) ''' Output: ('two', 2) ('three', 3) ('one', 1) ('four', 4) ''' |
2. Using dict.keys() function
Another simple solution to iterate over a dictionary in the sorted order of keys is to use the dict.keys() with the sorted() function.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
if __name__ == '__main__': d = {'one': 1, 'two': 2, 'three': 3, 'four': 4} for k in sorted(d.keys()): print((k, d[k])) ''' Output: ('four', 4) ('one', 1) ('three', 3) ('two', 2) ''' |
This is equivalent to:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
if __name__ == '__main__': d = {'one': 1, 'two': 2, 'three': 3, 'four': 4} for k in sorted(d): print((k, d[k])) ''' Output: ('four', 4) ('one', 1) ('three', 3) ('two', 2) ''' |
Alternatively, to iterate in reverse order of keys, you can specify the reverse argument of the sorted() function as True.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
if __name__ == '__main__': d = {'one': 1, 'two': 2, 'three': 3, 'four': 4} for k in sorted(d, reverse=True): print((k, d[k])) ''' Output: ('two', 2) ('three', 3) ('one', 1) ('four', 4) ''' |
Finally, to iterate the dictionary in sorted order of values, you can supply the key argument of the sorted() function.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
if __name__ == '__main__': d = {'three': 3, 'two': 2, 'four': 4, 'one': 1} for k in sorted(d, key=d.get): print((k, d[k])) ''' Output: ('one', 1) ('two', 2) ('three', 3) ('four', 4) ''' |
If the dictionary’s values are numeric, you can also use the Counter class from the collections module to iterate over the dictionary in decreasing order of their values.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
from collections import Counter if __name__ == '__main__': d = {'three': 3, 'two': 2, 'four': 4, 'one': 1} c = Counter(d) for p in c.most_common(): print(p) ''' Output: ('four', 4) ('three', 3) ('two', 2) ('one', 1) ''' |
That’s all about iterating over a dictionary in sorted order in Python.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)