Python: Iterate over a dictionary sorted by value

In this article, we will learn different ways to iterate over a python dictionary sorted by value in both the assending and descending order.

Iterate over a python dictionary sorted by value using sorted()

Iterate over dictionary sorted by value in ascending order

To iterate over a dictionary sorted by custom logic, we need to pass a custom comparator to 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.

Now to iterate over a dictionary sorted by value, we can pass a lambda function along with a dictionary to the sorted() function. This lambda function will be called for each pair of the dictionary while sorting and value returned by it will be used for comparision during sorting. This lambda function returns the second item of the pair received as argument, therefore key-value pairs of dictionary will be sorted by values. Let’s see an example,

# Dictionary of string and integers
word_freq = {
    'Hello' : 56,
    'At'    : 23,
    'Test'  : 43,
    'Why'   : 11,
    'This'  : 78,
}

# Iterate over a dictionary sorted by values
for key, value in sorted(word_freq.items(),
                        key=lambda item: item[1]):
    print(key, ' :: ', value)

Output:

Why  ::  11
At  ::  23
Test  ::  43
Hello  ::  56
This  ::  78

We iterated over all key-value pairs of dictionary sorted by key in ascending order.

Iterate over dictionary sorted by value in descending order

In previous example, we iterated over all pairs of dictionary sorted by values but in ascending order. Now to loop over all items sorted by values 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 key-value pairs of dictionary
# sorted by values
for key, value in sorted(   word_freq.items(), 
                            key=lambda item: item[1],
                            reverse=True):
    print(key, ' :: ', value)

Output

This  ::  78
Hello  ::  56
Test  ::  43
At  ::  23
Why  ::  11

With a dictionary and a reverse flag as True, the sorted() function returned a sequence of all key-value pairs of dictionary sorted by value in decreasing order. Then using a for loop we iterated over them.

Iterate over dictionary sorted by values using itemgetter()

Iterate over dictionary sorted by value in descending order

Instead of using a lambda function as comparator in the sorted() function, we can use operator.itemgetter() function to select value field from each pair for comparision while sorting. For example,

import operator

# Dictionary of string and integers
word_freq = {
    'Hello' : 56,
    'At'    : 23,
    'Test'  : 43,
    'Why'   : 11,
    'This'  : 78,
}

# Iterate over a dictionary sorted by values
for key, value in sorted(word_freq.items(),
                        key=operator.itemgetter(1)):
    print(key, ' :: ', value)

Output

Why  ::  11
At  ::  23
Test  ::  43
Hello  ::  56
This  ::  78

We iterated over all key-value pairs of dictionary sorted by key in ascending order.

Iterate over dictionary sorted by value in descending order

In previous example, we iterated over all pairs of dictionary sorted by values but in ascending order. Now to loop over all items sorted by values 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 key-value pairs of dictionary
# sorted by values
for key, value in sorted(word_freq.items(),
                        key=operator.itemgetter(1),
                        reverse=True):
    print(key, ' :: ', value)

Output

This  ::  78
Hello  ::  56
Test  ::  43
At  ::  23
Why  ::  11

With a dictionary and a reverse flag as True, the sorted() function returned a sequence of all key-value pairs of dictionary sorted by value in decreasing order. Then using a for loop we iterated over them.

Summary:

We learned about different ways to sort key-value pairs of dictionary by value.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top