Skip to content
Advertisement

Sort dictionary by the INT value of the value

There are many posts here about sorting dictionaries in Python so I was careful to read them and hope this is not a duplicate:

I’m using a dictionary to hold words as keys and the occurrence of the word as value. This leads to a dictionary that could be like:

John 1
Robert 5
Susie 15
Alex 6

I want to sort them by occurrence (the ‘value’)

John 1
Robert 5
Alex 6
Susie 15

I’m using the following code to try to sort my dictionary like this:

sorted_words = sorted(words.iteritems(), key=itemgetter(1))

However this returns a sorted list of tuples that looks like this:

John 1
Susie 15
Robert 5
Alex 6

You can see the problem is that with the above code the values are sorted “alphabetically”, so 15 follows 1, even though mathematically 15 > 5 and 6 and so should be last.

How can I fix the code to treat the values as INTs and not strings

Advertisement

Answer

You have to convert to values to integers in your key expression. Use

sorted_words = sorted(words.iteritems(), key=lambda x: int(x[1]))

It may be tempting to try something like key=int(itemgetter(1)), but this will not work since the key parameter is expecting a function.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement