Sort Dictionary by Value in Python
Learn via video course
Overview
A Python dictionary is a data structure that is mapped as key-value pairs. Initially, a dictionary is an unordered set of data, but we can sort it with a key or value. In this article, we will cover the different ways we can sort dictionary by value in Python.
Scope
This article covers below topics:
- Different ways of sorting a dictionary by key in Python.
- Sort Dictionary by Value in Python.
- dictionary in Python.
- how to sort a dictionary in Python using a For loop, Sorted() function, Lambda Function, operator Module, and itemgetter().
Introduction
The dictionary is one of the most important data structures in Python. It stores information in the form of key-value pairs. Every key in a dictionary has a unique value (no duplicate keys should be present in a dictionary). Every unique key is used to access an associated value present in the dictionary. We can access each value of a dictionary using its unique key in O(1) time.
Let’s say we need to create a list for any shopkeeper about their item sales quantity or the number of each item sold. You were thinking that we would create two lists, one for item name and one for item quantity sold out. You are right, but can we make it more optimal since we are using Python?
Yes, we can make it more optimal by using the dictionary so that we can easily sort dictionary by value in Python.
We can create a dictionary that has items as keys and their quantities sold as values. This is what the Python dictionary looks like:
We can see in the above dictionary that we have items and their sold quantities. We have sold 12 bread packets, 10 biscuit packets, and so on. Now we have the shop items dictionary, we can access any item quantity by item name.
Output:
Now, let's say we want to sort out dictionaries based on their quantity. Here comes the concept of sorting the dictionary. We can sort the dictionary by its keys and values. Let's have a look at how we can sort dictionary by value in Python.
Sort a Dictionary in Python Using a For loop
A for loop can easily sort a dictionary in Python by just sorting the values in the dictionary and then creating a new dictionary based on the sorted order of values present in the dictionary.
If we talk about the time and space complexity of the above approach, then the time complexity would be , whereas the space complexity would be .
This is not an in-place sort as we are creating a new dictionary to add the sorted values. Let's have a look at the example given below:
Code:
Output:
In the above example, we can see that first we have created a list of all the values in our dictionary and then we start traversing through all the values along with traversing the keys of the dictionary every time. We will perform matching of the keys and values, and finally, we will insert them into a new dictionary in the sorted form. At last, we have sort dictionary by value in Python.
Sort a Dictionary in Python Using Sorted() Function
Python is a function-enriched language. we can easily sort dictionary by value in Python. It has an inbuilt function sorted that can be used to sort a dictionary by using a parameter key. The key parameter in the sorted function is used to tell the function the deciding value according to which dictionary needs to be sorted.
To sort the dictionary using the sorted function, we will pass the value dict_name.get as the value for the parameter key. This will tell the sorted function that we need to sort the keys according to the values we get from the dictionary.
The primary logic behind the approach is to create a list of keys that are sorted according to their values and then form a sorted dictionary.
Code:
Output:
Sort a Dictionary in Python Using Lambda Function
To sort a function using the lambda function, we will also need to use the sorted function. A lambda function is used as a value for the key parameter in the sorted function. As we discussed earlier, that key parameter in the sorted function defines the value through which sorting takes place. Here the lambda function works on getting that value.
We also use the .items() method in dictionary, which is used to create a list of tuples that represent a dictionary.
The primary logic behind this approach is to create a list of tuples and then sort them using sorted and lambda functions by using each tuple's second value as the key and then convert the sorted list of tuples into a new dictionary.
Code:
Output:
Sort a Dictionary In Python Using the Operator Module and itemgetter()
To sort a function using the operator module and itemgetter() function, we will also need to use the sorted function. The operator module and itemgetter() functions are used to define a value for the key parameter in the sorted function. As we discussed earlier, that key parameter in the sorted function defines the value through which sorting takes place. Here the lambda function works on getting that value.
We also use the .items() method in Dictionary, which is used to create a list of tuples that represent a dictionary. We also need to import the Operator Module before using it in the sorted function.
The Main Logic Behind this approach is to create a list of tuples and then sort them using the sorted, operator module, and itemgetter() Function by using each tuple's second value as the key and then converting the sorted list of tuples into a new dictionary.
Note: This method is very similar to sorting a dictionary in Python using the lambda function. The only difference between them is that instead of using lambda to get the second value for each tuple in the list of tuples, we use the Itemgetter function to access the 1st index element, i.e., the second element for each tuple to sort dictionary by value in Python
Code:
Output:
Conclusion
Let's conclude our topic, sort dictionary by value in Python.
- We have seen how easily we can sort dictionary by value in Python.
- A Python dictionary stores key-value pair mappings, where the key must be unique.
- A dictionary can be sorted by both keys and values using the sorted() method.
- The sorted() method takes 3 parameters - object, key and reverse.
- sorted() sorts the objects in ascending order by default. To sort the object in reverse order, we set the reverse parameter in sorted() method to True.
- A dictionary can be sorted using predefined methods like dict.values() and dict.items() as well as by custom sort functions.
- dict.items() returns the key-value pairs of a dictionary as a list of tuples whereas dict.values() returns only the values as a list.
- dict.items() is more efficient than dict.values() for sorting.
- The operator module and itemgetter() functions are used to define a value for the key parameter in the sorted function.