Register Login

How to Sort a Dictionary in Python?

Updated Mar 27, 2020

Python Sorted Dictionary

In this article, we will learn how to sort dictionary in python. Dictionaries in Python are used to store key-value pairs in disordered (not in order) manner. You can also sort or order dictionary in ascending /descending order using built-in python function sorted().

There are three ways for sorting a dictionary in python:

Python Sort Dictionary by key

In this example we are using a sorted() function to sort the dictionary and for loop to print the item of the dictionary.

Example 1

# Python3
# Sort or Order dictionary with key-value pairs.

# Initialized a dictionary
fruitscolor = {"Banana" : "Yellow",
    "Mango" : "Green",
    "Apple" : "Red",
    "Grapefruit" : "Pink",
    "Blackberry" : "Purple",
    "Sapodilla" : "Brown"}

# sort and print the items of dictionary
for fruit, color in sorted(fruitscolor.items()):
  print(fruit, ":", color)

Output

Apple : Red
Banana : Yellow
Blackberry : Purple
Grapefruit : Pink
Mango : Green
Sapodilla : Brown

Explanation

In the above example, we initialized a dictionary with name “fruitscolor”. This dictionary contains the name of fruit as ‘key’ and their respective color as ‘values’. Then we used the sorted() method to sort the items of the dictionary. This method returns a sorted list of our dictionary in alphabetical order.

Example 2

# Python3
# Sort or Order dictionary by key.

# Initialized a dictionary
fruitscolor = {"Banana" : "Yellow",
    "Mango" : "Green",
    "Apple" : "Red",
    "Grapefruit" : "Pink",
    "Blackberry" : "Purple",
    "Sapodilla" : "Brown"}

# sort items in dictionary
fruitscolor = sorted(fruitscolor)

# Print items in dictionary through for loop
for fruit in fruitscolor:
 print(fruit)

Output

Apple
Banana
Blackberry
Grapefruit
Mango
Sapodilla

Explanation

In the above example, we first initialized our dictionary then sorted it using sorted() method. Now the fruitscolor holds a sorted and updated dictionary. Then we used for loop for looping only through keys I.e ‘fruit’ name. Thus we got only the sorted output of fruit name and not the color.   

Python Sort Dictionary by Value

To sort the dictionary by the value, we use value() function with sorted() function and to print the value we use ‘for loop’.

Example

# Python3
# Sort or Order dictionary by values.

# Initialized a dictionary
fruitscolor = {"Banana" : "Yellow",
    "Mango" : "Green",
    "Apple" : "Red",
    "Grapefruit" : "Pink",
    "Blackberry" : "Purple",
    "Sapodilla" : "Brown"}

# sort values in dictionary
fruitscolor = sorted(fruitscolor.values())

# Print values in dictionary through for loop
for color in fruitscolor:
  print(color)

Output

Brown
Green
Pink
Purple
Red
Yellow

Explanation

In the above example, we used sorted() and values() inbuilt methods. The sorted() methods returns the sorted iterable object. Whereas values() method returns a list of all values in a dictionary. The fruitscolor holds the sorted values of dictionary. At last we used for loop for iterating over the values of dictionary. Thus the output only contain values and not the keys.

Reversing the Sort Order

To change the sort order, we need to use “reverse=True” option with sorted() function.

We can use this method to explain all of the scenarios as mentioned above, such as sort by key, sort by key-value pair & sort by value.

Dictionary Sort by Key or Key-value Pairs in Reverse Order

Example

# Python3
# Sort or Order dictionary by key in reverse order.

# Initialized a dictionary
fruitscolor = {"Banana" : "Yellow",
    "Mango" : "Green",
    "Apple" : "Red",
    "Grapefruit" : "Pink",
    "Blackberry" : "Purple",
    "Sapodilla" : "Brown"}

# Sort dictionary in reverse order
fruitscolor = sorted(fruitscolor, reverse=True)

# Print items in dictionary through for loop
for fruit in fruitscolor:
 print(fruit)

Output

Sapodilla
Mango
Grapefruit
Blackberry
Banana
Apple

Explanation

In the above example, we just added an extra parameter to sorted() method I.e “reverse=True. The reverse accepts boolean value either true or false. If it is set equal to true, it’ll sort in descending order else ascending order. The reverse is optional and it’s default value is false.

In this case, reverse is set to ‘True’. Thus the sorted output we got is in reverse order.   

Conclusion

To sort a dictionary we can use a built-in method sorted(). We can sort the dictionary either by it’s value or by it’s key. To sort by values we use a built-in method values(). To sort in reverse order add an extra parameter to sorted() method reverse and set it equal to ‘true’. 


×