Python How to sort items in dictionary 5 examples

In Python you can get the items of dictionary sorted by key or by value, ascending or descending, dictionary of list. BY default dictionaries are not sorted so you can get the items sorted in some order but the dictionary will remain unsorted. Since dictionaries don't have order you need to use representation like list of tuples in order to get the elements in some order

In this post sorting python 3 dictionary:

  • sort a dict by key
  • sort a dictionary by value ascending order
  • sort a map by value descending order
  • sort a list of dictionaries
  • sort a dictionary of lists by Nth item

Sort a dictionary by key

The most common operation is to get dictionary values sorted by keys. This can be done by using method sorted. The example below sorts the string keys in alphabetic order:

mydict = {"alpha": 3, "bravo": 4, "charlie": 5, "delta": 1, "echo": 2}
for k, v in sorted(mydict.items()):
    print(k, v)

result:

alpha 3
bravo 4
charlie 5
delta 1
echo 2

Sort a dictionary by value ascending order

Another very often operation is to sort dictionary items by value in ascending order. Below you can find how to sort dictionary(map or dict) using the integer values and reverse=False:

mydict = {"alpha": 3, "bravo": 4, "charlie": 5, "delta": 1, "echo": 2}
s = [(k, mydict[k]) for k in sorted(mydict, key=mydict.get, reverse=False)]
for k, v in s:
    print(k, v)

result:

delta 1
echo 2
alpha 3
bravo 4
charlie 5

Sort a dictionary by value descending order

Sorting in descending order is simple and you need only to change parameter reverse to True:

mydict = {"alpha": 3, "bravo": 4, "charlie": 5, "delta": 1, "echo": 2}
s = [(k, mydict[k]) for k in sorted(mydict, key=mydict.get, reverse=True)]
for k, v in s:
    print(k, v)

result:

charlie 5
bravo 4
alpha 3
echo 2
delta 1

Sort a list of dictionaries

If you have more complex structure like list of dictionaries and you want to sort them by a given property then you can convert this dictionary to list of tuples and then sort the items and output the sorted elements.

list_to_be_sorted = [{'team':'alpha', 'score':99}, {'team':'bravo', 'score':55}, {'team':'charlie', 'score':74}]
newlist = sorted(list_to_be_sorted, key=itemgetter('score'))
print(newlist)

result:

[{'score': 55, 'team': 'bravo'}, {'score': 74, 'team': 'charlie'}, {'score': 99, 'team': 'alpha'}]

Sort a dictionary of lists by Nth item

You can have dictionary with values lists which you want to sorted by Nth element of the lists. This can be done by using simple lambda expression like. This example sorts by the first element of each list

myDict = { 'alpha' : [ 1, 5, 4], 'beta' : [3, 2, 9], 'charlie' : [ 7, 0, 6 ] }
sorted(myDict.items(), key=lambda e: e[1][2])

for key, value in sorted(myDict.items(), key=lambda e: e[1][0]):
    print(key, value)

result:

alpha [1, 5, 4]
beta [3, 2, 9]
charlie [7, 0, 6]

You can sort by second or third element simply by changing the index of the element:

for key, value in sorted(myDict.items(), key=lambda e: e[1][1]):
    print(key, value)
for key, value in sorted(myDict.items(), key=lambda e: e[1][2]):
    print(key, value)

result:

charlie [7, 0, 6]
beta [3, 2, 9]
alpha [1, 5, 4]

alpha [1, 5, 4]
charlie [7, 0, 6]
beta [3, 2, 9]

Resources

Related Article