Sort a dictionary by value in Python: Useful guide you need to know

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 value by value Python.

Why you need to sort a dictionary?

  • By sorting the dictionary, 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 sort a dictionary by value Python. 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 sort a 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

Sort a dictionary by value python

Output will be 

espresso 72

latte 56

cappuccino 54

americano 48

cortado 41

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[1]

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 sort the dictionary in ascending order. Thanks to that, you can, for example, find the least popular drink in your store. Let’s look at this following program

Sort a dictionary by value python

This program is quite similar to the previous one. However, when sorting the dictionary in ascending order, we get rid of the reverse = True parameter. Then, the output will be

cortado 41

americano 48

cappuccino 54

latte 56

espresso 72

List Comprehension

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 sort the dictionary in ascending order with list comprehension

The output will be as follow

cortado 41

americano 48

cappuccino 54

latte 56

espresso 72

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.

In conclusion

Hopefully through this guideline and tutorial, you’ll know how to sort a dictionary by value in Python. However, Python is not easy to work with, especially for beginners. Therefore, to avoid problems and unexpected issues, we highly recommend contacting ArrowHitech – one of the best outsourcing IT company in Vietnam

Moreover, we can also help you with your website as well as online business. With more than 12 years in e-commerce, we can grant you an outstanding and professional-looking web store, regardless of the eCommerce platforms. Working with our decade-long experienced developers, you surely get your dream online shop. What are you waiting for? Contact us here or via email: support@arrowhitech.com or hotline + 84 243 7955 813

Tags

Share