In this article, we will learn different ways to iterate over a python dictionary sorted by key in both the assending and descending order.
Table of Contents:
- Iterate over a python dictionary sorted by key using sorted().
- Iterate over dictionary sorted by keys using items().
- Iterate over dictionary sorted by key using comparator / lambda function
Iterate over a python dictionary sorted by key using sorted()
Iterate over dictionary sorted by key in ascending order
Dictionary objects in python can also be used as an iterator object, to iterate over all keys of the dictionary. Therefore, if we pass the dictionary object to the sorted() function, then it returns a sorted iterable sequence of all keys of dictionary. Then we can iterate over this sorted sequence using a for loop and also select the value associated with the key, it will give use an effect that we have iterated over all pairs of dictionary sorted by key. For example,
# Dictionary of string and integers word_freq = { 'Hello' : 56, 'At' : 23, 'Test' : 43, 'Why' : 11, 'This' : 78, } # Iterate over a dictionary sorted by key in ascending order for key in sorted(word_freq): print(key, ' :: ', word_freq[key])
Output
At :: 23 Hello :: 56 Test :: 43 This :: 78 Why :: 11
Here we passed the dict object word_freq to the sorted() function and it returned an iterable sequence of all keys of dictionary but in sorted manner. Then we used that sorted sequence to iterate over all key-value items of dictionary word_freq sorted by key in ascending order and printed them one by one.
Iterate over dictionary sorted by key in descending order
In previous example, we iterated over all pairs of dictionary sorted by keys but in ascending order. Now to loop over all items sorted by keys in decreasing order, just pass the reverse flag as True in the sorted() function. For example,
# Dictionary of string and integers word_freq = { 'Hello' : 56, 'At' : 23, 'Test' : 43, 'Why' : 11, 'This' : 78, } # Iterate over a dictionary sorted by key in descending order for key in sorted(word_freq, reverse=True): print(key, ' :: ', word_freq[key])
Output
Frequently Asked:
- Python Dictionary update()
- Python: Iterate over a dictionary sorted by value
- Python: Dictionary with multiple values per key
- Python dict pop()
Why :: 11 This :: 78 Test :: 43 Hello :: 56 At :: 23
With a dictionary and a reverse flag as True, the sorted() function returned a sequence of all keys of dictionary sorted in decreasing order. Then using a for loop we iterated over them.
Iterate over dictionary sorted by keys using items()
The items() function of a dictionary returns an iterable sequence of key-value tuples of dictionary. If we pass this sequence to the sorted() function, then we can get an iterable sequence of all key-value pairs sorted by key. For example,
# Dictionary of string and integers word_freq = { 'Hello' : 56, 'At' : 23, 'Test' : 43, 'Why' : 11, 'This' : 78, } # Iterate over a dictionary sorted by key for key, value in sorted(word_freq.items()): print(key, ' :: ', value)
Output
At :: 23 Hello :: 56 Test :: 43 This :: 78 Why :: 11
Here, we iterated over all key-value pairs of dictionary sorted by key.
How to iterate in reverse order?
In addition to the sequence returned by items(), by passing the reverse flag as True to the sorted() function, we can get an iterable sequence of all items of dictionary sorted by key in decreasing order. For example,
word_freq = { 'Hello' : 56, 'At' : 23, 'Test' : 43, 'Why' : 11, 'This' : 78, } # Iterate over a dictionary sorted by key for key, value in sorted(word_freq.items(), reverse=True): print(key, ' :: ', value)
Output
Why :: 11 This :: 78 Test :: 43 Hello :: 56 At :: 23
Iterate over dictionary sorted by key using Lambda function as comparator
Suppose we want to iterate over a dictionary sorted by key, but comparision logic should not be the default one. For that we need to pass a comparator function in the sorted() function. This comparator function will be used to compare the key-value pairs of dictionary while sorting. We can also pass a lambda function as the comparator.
For example, we have a dictionary with strings as keys, now we want to iterate over dictionary items sorted by the length of keys instead of alphabetical order of key strings. Let’s see how to do that using a lambda function,
# Dictionary of string and integers word_freq = { 'Hello' : 56, 'At' : 23, 'Test' : 43, 'Why' : 11, 'This' : 78, } # Iterate over a key-value pairs of dictionary # sorted by key for key, value in sorted( word_freq.items(), key=lambda item: len(item[0])): print(key, ' :: ', value)
Output:
At :: 23 Why :: 11 Test :: 43 This :: 78 Hello :: 56
We passed lambda function to the sorted() function as argument along with a dictionary. This lambda function was called for each pair of the dictionary while sorting and value returned by it was used for comparision during sorting. This lambda function returned the length of first item of the pair, therefore key-value pairs of dictionary were sorted by the length of key strings.
Summary:
We learned about different ways to sort key-value pairs of dictionary by key.