How to sort a dictionary by values in Python

By definition, dictionary are not sorted (to speed up access). Let us consider the following dictionary, which stores the age of several persons as values:

If you want to sort this dictionary by values (i.e., the age), you must use another data structure such as a list, or an ordered dictionary.

Use the sorted function and operator module

Sorted_d is a list of tuples sorted by the second element in each tuple. Each tuple contains the key and value for each item found in the dictionary. If you look at the content of this variable, you should see:

Use the sorted function and lambda function

If you do not want to use the operator module, you can use a lambda function:

The computation time is of the same order of magnitude as with the operator module. Would be interesting to test on large dictionaries.

Use the sorted function and return an ordered dictionary

In the previous methods, the returned objects are list of tuples. So we do not have a dictionary anymore. You can use an OrderedDict if you prefer:

Use sorted function and list comprehension

Another method consists in using list comprehension and use the sorted function on the tuples made of (value, key).

Here the output is a list of tuples where each tuple contains the value and then the key:

A note about Python 3.6 native sorting

In previous version on this post, I wrote that “In Python 3.6, the iteration through a dictionary is sorted.”. This is wrong. What I meant is that in Python 3.6 dictionary keeps insertion sorted.

It means that if you insert your items already sorted, the new python 3.6 implementation will be this information. Therefore, there is no need to sort the items anymore. Of course, if you insert the items randomly, you will still need to use one of the method mentioned above.

For instance, taking care of the age, we now create our list as follows (sorting by ascending age):

Now you can iterate through the items and they will be in the same order as in the creation of the dictionary. So you can just create a list from your items very easily:

Benchmark

Here is a quick benchmark made using the small dictionary from the above examples. Would be interesting to redo the test with a large dictionary.

What you can see is that the native Python dictionary sorting is pretty cool followed by the combination of the lambda + list comprehension method. Overall using one of these methods would be equivalent though (factor 2/3 at most).

This image was created with the following code.

This entry was posted in Python, Uncategorized and tagged , . Bookmark the permalink.

4 Responses to How to sort a dictionary by values in Python

  1. Chris says:

    In the py36 example, you’re not actually sorting anything, which is why it’s significantly faster.

  2. Michal Ambroz says:

    Hello,
    the python36 way doesn’t seem to sort. I am using ipython3:

    > import import sys
    > sys.version
    ‘3.6.6 (default, Jul 19 2018, 14:25:17) \n[GCC 8.1.1 20180712 (Red Hat 8.1.1-5)]’

    > d = {“Pierre”: 42, “Anne”: 33, “Zoe”: 24}
    > sorted_d = [(k,v) for k,v in d.items()]
    > print(sorted_d)
    [(‘Pierre’, 42), (‘Anne’, 33), (‘Zoe’, 24)]

    What is wrong?
    Thank you

    • Looking back at this post, I don’t know why I wrote that py36 was sorting natively. This is wrong indeed.

      What’s important to know about Python 3.6 and above is that dictionary keeps insertion sorted.

      So if the dictionary is not sorted, which is the case in this example, well the dictionary is not sorted ! So, you still need to use one of the sorting method explained in the text. However, if you took care of inserting items already sorted, then there is nothing to do, you can iterate through the dictionary in the same order. See the updated post and thanks for reporting this error.

    • Emmanuel says:

      I think you do:
      sorted_d = [(k,v) for k,v in d.items()]

      According to the article, it should be:
      sorted_d = [(v,k) for k,v in d.items()]

      v,k not k,v in the first tuple.

Leave a Reply

Your email address will not be published.