Python dictionary is a collection of unordered data structures. It uses a mapping structure to store data, and is stored in the key – value form. Then, in this guideline, we will give you different methods to sort a dictionary by value python
Why you need to order dictionary by value?
- By order dictionary by value, you can use the dictionary in place for lists
- Moreover, sorting helps you analyze the data a lot more efficiently, especially when you’re working with the data – structure
- The data will look a lot cleaner. Thus, you can handle complex operations
Sort a dictionary by value Python
Now, in this guideline, we will focus on how to order dictionary by value. In case you want to sort the dictionary by key – value pairs, then you can use two following functions simultaneously: item () and sorted ()
The item () function helps you to retrieve the item in the dictionary. While sorted () function and a custom key parameter can order dictionary by value. Now, let’s consider these following examples
Sort a dictionary by value Python in Descending Order
Imagine we have a coffee shop. The dictionary stores the menu of your drink along with the number of orders for each one during last month. Then, to see the most popular drink last month, we can sort the dictionary in descending order. Then, here is the program for sorting in descending order
Output will be
Let’s look into this code.
The dictionary named “ order” holds the names of drinks as keys, and the number of orders as values. In this example, we use the sorted () method to sort the dictionary in descending order. Then, here’s how it works
- Parameter: Object:
Text: orders.items ()
This indicates all the values in your “ orders” dictionary, not merely “ orders”. If you only use “ orders”, you’ll have to reference the index position of the items to get its individual value.
- Parameter: key
Text: key=lambda x:x
This is a sorting mechanism. With this, you’ll be able to sort the dictionary by value. In this example, we use Lambda function – a function without a name
- Parameter: Reverse
Text: reverse= True
Set this to have your data sorted in descending order
After that, we create a “ for” loop. This will loop through each item in the “ sort_order” method. Then, it prints out both its key name and values, which are sorted in the order we have specified earlier
Sort a dictionary Python in ascending order
In this part, we still use the same example as the previous one – coffee shop. However, contrary to the previous one, now, we will order dictionary by value follow ascending order. Thanks to that, you can, for example, find the least popular drink in your store. Let’s look at this following program
This program is quite similar to the previous one. However, when ordering dictionary by value in ascending order, we get rid of the reverse = True parameter. Then, the output will be
Another way to sort a dictionary by value Python using list comprehension. This method will save you a lot more space, which is very helpful if you’re creating more complex sort methods.
Using the same “ Coffee shop” example, here’s the code to order dictionary by value in ascending order with list comprehension
The output will be as follow
This one might look similar to the above one. However, instead of defining a sort_orders variable and creating a separate for loop, we create a list using the list comprehension method.
Hopefully through this guideline and tutorial, you’ll know how to sort a dictionary by value in Python. However, to best avoid problems and unexpected issues, you should contact experts and professional developers. In this case, we highly recommend ArrowHitech – one of the best outsourcing IT company in Vietnam with Mobile application development services. Contact us here !