To sort a dictionary by value in Python you can use the Show Dictionaries are unordered data structures. They use a mapping structure to store data. Dictionaries map keys to values, creating pairs that hold related data. Find Your Bootcamp Match
First name Last name Phone number By continuing you agree to our Terms of Service and Privacy Policy, and you consent to receive offers and opportunities from Career Karma by telephone, text message, and email. Using the Python Python sorted() RefresherPython’s built-in Here’s the syntax for the sorted(object, key, reverse) The method takes in three parameters:
As you can see, orders = { 'cappuccino': 54, 'latte': 56, 'espresso': 72, 'americano': 48, 'cortado': 41 } sort_orders = sorted(orders.items(), key=lambda x: x[1], reverse=True) for i in sort_orders: print(i[0], i[1])3is the only required parameter. If you decide not to use the optional orders = { 'cappuccino': 54, 'latte': 56, 'espresso': 72, 'americano': 48, 'cortado': 41 } sort_orders = sorted(orders.items(), key=lambda x: x[1], reverse=True) for i in sort_orders: print(i[0], i[1])4 and orders = { 'cappuccino': 54, 'latte': 56, 'espresso': 72, 'americano': 48, 'cortado': 41 } sort_orders = sorted(orders.items(), key=lambda x: x[1], reverse=True) for i in sort_orders: print(i[0], i[1])5 parameters, Python will automatically sort the object in ascending order. Note: Career Karma wrote a full guide on the sort() and sorted() methods in Python. If you’re looking to learn more about this method and the orders = { 'cappuccino': 54, 'latte': 56, 'espresso': 72, 'americano': 48, 'cortado': 41 } sort_orders = sorted(orders.items(), key=lambda x: x[1], reverse=True) for i in sort_orders: print(i[0], i[1])6 parameter, check out our Python sort() tutorial. Let’s walk through a quick example to illustrate how the sorted() method works. » MORE: Python TabError: inconsistent use of tabs and spaces in indentation Solution Say that we are operating a coffee shop and we want to retrieve an alphabetical list of our orders = { 'cappuccino': 54, 'latte': 56, 'espresso': 72, 'americano': 48, 'cortado': 41 } sort_orders = sorted(orders.items(), key=lambda x: x[1], reverse=True) for i in sort_orders: print(i[0], i[1])7 (loyalty) customers. We already have a list of customers, but it is ordered by sign-up date. We could use the following code to sort our list: customers = ['Kaley Fernandez', 'Darius Rowland', 'Isaac Borthwick', 'Alexandria Kidd'] sorted_customers = sorted(customers) print(sorted_customers) Our code sorts the orders = { 'cappuccino': 54, 'latte': 56, 'espresso': 72, 'americano': 48, 'cortado': 41 } sort_orders = sorted(orders.items(), key=lambda x: x[1], reverse=True) for i in sort_orders: print(i[0], i[1])8 array and returns the following: orders = { 'cappuccino': 54, 'latte': 56, 'espresso': 72, 'americano': 48, 'cortado': 41 } sort_orders = sorted(orders.items(), key=lambda x: x[1], reverse=True) for i in sort_orders: print(i[0], i[1])9 On the first line of our code, we declare a list that stores our customers’ names; this list is called: customers. Then, we use the orders = { 'cappuccino': 54, 'latte': 56, 'espresso': 72, 'americano': 48, 'cortado': 41 } sort_orders = sorted(orders.items(), key=lambda x: x[1]) for i in sort_orders: print(i[0], i[1])1. Finally, we print out the newly sorted list to the console using the orders = { 'cappuccino': 54, 'latte': 56, 'espresso': 72, 'americano': 48, 'cortado': 41 } sort_orders = sorted(orders.items(), key=lambda x: x[1]) for i in sort_orders: print(i[0], i[1])2 function. Sort a Dictionary by ValueLet’s say that you have a dictionary and you want to sort it by key-value pairs. You can do this by using two functions together: orders = { 'cappuccino': 54, 'latte': 56, 'espresso': 72, 'americano': 48, 'cortado': 41 } sort_orders = sorted(orders.items(), key=lambda x: x[1]) for i in sort_orders: print(i[0], i[1])3and sorted() . The orders = { 'cappuccino': 54, 'latte': 56, 'espresso': 72, 'americano': 48, 'cortado': 41 } sort_orders = sorted(orders.items(), key=lambda x: x[1]) for i in sort_orders: print(i[0], i[1])3 function allows you to retrieve the items in a dictionary. We can use this function in combination with the sorted() function and a custom orders = { 'cappuccino': 54, 'latte': 56, 'espresso': 72, 'americano': 48, 'cortado': 41 } sort_orders = sorted(orders.items(), key=lambda x: x[1], reverse=True) for i in sort_orders: print(i[0], i[1])6 parameter to sort a dictionary by value. Consider the following two examples. Example 1: Sort in Descending OrderLet’s return to the coffee shop. Suppose we have a dictionary that stores the items on our coffee menu as well as how many of each item were ordered in the last month. We want to see what the most popular coffee was last month, so we decide to sort the order dictionary in descending order of values. Here’s a program we could use to sort the contents of our dictionary by value: orders = { 'cappuccino': 54, 'latte': 56, 'espresso': 72, 'americano': 48, 'cortado': 41 } sort_orders = sorted(orders.items(), key=lambda x: x[1], reverse=True) for i in sort_orders: print(i[0], i[1]) Our code returns the following: » MORE: 'Python' is not recognized as an internal or external command, operable program or batch file orders = { 'cappuccino': 54, 'latte': 56, 'espresso': 72, 'americano': 48, 'cortado': 41 } sort_orders = sorted(orders.items(), key=lambda x: x[1]) for i in sort_orders: print(i[0], i[1])8 orders = { 'cappuccino': 54, 'latte': 56, 'espresso': 72, 'americano': 48, 'cortado': 41 } sort_orders = sorted(orders.items(), key=lambda x: x[1]) for i in sort_orders: print(i[0], i[1])9 orders = { 'cappuccino': 54, 'latte': 56, 'espresso': 72, 'americano': 48, 'cortado': 41 } [print(key, value) for (key, value) in sorted(orders.items(), key=lambda x: x[1])0 orders = { 'cappuccino': 54, 'latte': 56, 'espresso': 72, 'americano': 48, 'cortado': 41 } [print(key, value) for (key, value) in sorted(orders.items(), key=lambda x: x[1])1 orders = { 'cappuccino': 54, 'latte': 56, 'espresso': 72, 'americano': 48, 'cortado': 41 } [print(key, value) for (key, value) in sorted(orders.items(), key=lambda x: x[1])2 There’s a lot going on in our code, so let’s break it down. At the start of our code, we define a dictionary called orders = { 'cappuccino': 54, 'latte': 56, 'espresso': 72, 'americano': 48, 'cortado': 41 } [print(key, value) for (key, value) in sorted(orders.items(), key=lambda x: x[1])3 that stores the names of coffees as keys and the number sold as values. Then, we use the orders = { 'cappuccino': 54, 'latte': 56, 'espresso': 72, 'americano': 48, 'cortado': 41 } [print(key, value) for (key, value) in sorted(orders.items(), key=lambda x: x[1])3 dictionary by value. Here’s a breakdown of how we used the sorted() method:"Career Karma entered my life when I needed it most and quickly helped me match with a bootcamp. Two months after graduating, I found my dream job that aligned with my values and goals in life!" Venus, Software Engineer at Rockbot Find Your Bootcamp MatchFinally, we create a orders = { 'cappuccino': 54, 'latte': 56, 'espresso': 72, 'americano': 48, 'cortado': 41 } [print(key, value) for (key, value) in sorted(orders.items(), key=lambda x: x[1])7 loop that loops through each item created in our orders = { 'cappuccino': 54, 'latte': 56, 'espresso': 72, 'americano': 48, 'cortado': 41 } [print(key, value) for (key, value) in sorted(orders.items(), key=lambda x: x[1])8method and prints out both its key name and its value, sorted in the order we specified in the orders = { 'cappuccino': 54, 'latte': 56, 'espresso': 72, 'americano': 48, 'cortado': 41 } [print(key, value) for (key, value) in sorted(orders.items(), key=lambda x: x[1])8 function. Example 2: Sort in Ascending OrderSimilarly, if we wanted to find out the least popular drink sold at our coffee shop, we could use the same code as above but without the orders = { 'cappuccino': 54, 'latte': 56, 'espresso': 72, 'americano': 48, 'cortado': 41 } sort_orders = sorted(orders.items(), key=lambda x: x[1]) for i in sort_orders: print(i[0], i[1]) When we run our code, the following values are returned: » MORE: Python Remove Character from String: A Guide
orders = { 'cappuccino': 54, 'latte': 56, 'espresso': 72, 'americano': 48, 'cortado': 41 } [print(key, value) for (key, value) in sorted(orders.items(), key=lambda x: x[1])1 orders = { 'cappuccino': 54, 'latte': 56, 'espresso': 72, 'americano': 48, 'cortado': 41 } [print(key, value) for (key, value) in sorted(orders.items(), key=lambda x: x[1])0 orders = { 'cappuccino': 54, 'latte': 56, 'espresso': 72, 'americano': 48, 'cortado': 41 } sort_orders = sorted(orders.items(), key=lambda x: x[1]) for i in sort_orders: print(i[0], i[1])9
As you can see, our code returned a list of items arranged in ascending order, based on the number of each item ordered in the last month. List ComprehensionIn addition, we can use list comprehension to sort dictionary contents by value. List comprehension is a concise technique to create lists in Python and can save space if you are creating more complex sort methods. Here’s the code we would use to sort our coffee orders in ascending order by the number of each coffee that was ordered using list comprehension: orders = { 'cappuccino': 54, 'latte': 56, 'espresso': 72, 'americano': 48, 'cortado': 41 } [print(key, value) for (key, value) in sorted(orders.items(), key=lambda x: x[1]) When we run our code, the following response is returned:
orders = { 'cappuccino': 54, 'latte': 56, 'espresso': 72, 'americano': 48, 'cortado': 41 } [print(key, value) for (key, value) in sorted(orders.items(), key=lambda x: x[1])1 orders = { 'cappuccino': 54, 'latte': 56, 'espresso': 72, 'americano': 48, 'cortado': 41 } [print(key, value) for (key, value) in sorted(orders.items(), key=lambda x: x[1])0 orders = { 'cappuccino': 54, 'latte': 56, 'espresso': 72, 'americano': 48, 'cortado': 41 } sort_orders = sorted(orders.items(), key=lambda x: x[1]) for i in sort_orders: print(i[0], i[1])9
The result of our code was the same as our above example where we sorted the contents of the orders = { 'cappuccino': 54, 'latte': 56, 'espresso': 72, 'americano': 48, 'cortado': 41 } [print(key, value) for (key, value) in sorted(orders.items(), key=lambda x: x[1])3list in ascending order. But instead of defining a sorted() 2 variable and creating a separate orders = { 'cappuccino': 54, 'latte': 56, 'espresso': 72, 'americano': 48, 'cortado': 41 } [print(key, value) for (key, value) in sorted(orders.items(), key=lambda x: x[1])7 loop to iterate through the sorted list, we created a list using the list comprehension technique. The list comprehension we created above sorts through each item in our list in ascending order, then prints out the key and value of each dictionary item to the console. ConclusionWhen you’re working with dictionaries in Python, sorting a dictionary by value is a common operation. The This tutorial discussed, providing examples, how to use the orders = { 'cappuccino': 54, 'latte': 56, 'espresso': 72, 'americano': 48, 'cortado': 41 } sort_orders = sorted(orders.items(), key=lambda x: x[1], reverse=True) for i in sort_orders: print(i[0], i[1])6and sorted() 7parameters. Now you’re ready to start sorting dictionaries by value like a Python pro! About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication. |