How to Convert Dict_Values to List in Python

In this Python tutorial, you will learn how to convert dict_values to list in Python using different methods with practical examples by taking real-time scenarios.

While working on a Python project for the restaurant’s Billing management system, I stored the data in a dictionary(key-value pair), where the key was the food item name and value as its price.

I needed to collect all the values in one list so I could make some calculations on it. So, I searched for a solution and found multiple approaches to get the dictionary values in a list in Python.

4 Ways to Convert Dict_Values to List in Python

Let me explain the scenario with one example so you will understand precisely where to use the methods.

Here’s a data in key-value pairs,

food_menu = {
    "Pizza": 50,
    "Chinese": 30,
    "Pasta": 45,
    "Burger": 64
}

So the data is stored in this way, and I need to get the list of all values of the dictionary in Python, and the output will look like this,

[50, 30, 45, 64]

Let’s understand all methods and techniques one by one with practical examples to Convert Dict_Values to List in Python

Convert Dict_Values to List Using dict.values() Method

First, we will use dict.values() method, which is used to extract all the values from the dictionary like this: dict_values([val1, val2….]). To convert it into a list, we will use the list constructor.

READ:  How to Swap Two Numbers in Python Using Function [4 Examples]

Syntax

list(dict_name.values())
  • values(): Make sure that you are using this method for the dictionary only; otherwise, it will give an error.
  • list() constructor: It will convert the extracted dictionary values to the list.

Here’s an example that shows how to convert dict_values to list in Python using dict.values() method

food_menu = {
    "Pizza": 50,
    "Chinese": 30,
    "Pasta": 45,
    "Burger": 64
}
price_list = list(food_menu.values())
print(price_list)
How to Convert Dict_Values to List in Python using dict.values() method

The above code is a dictionary of food_menu, where the key is the food item, and the value is its price. We also needed to extract all the dictionary values from the list. ,447/Convert Dictionary Values to List Python using for loop.

Convert Dictionary Values to List Python using for loop

We can also use a for loop in Python to convert a dictionary value to a list. First, it will get the dict’s values using the values() method. Then, it will iterate over every value one by one and append it to the list using the append() method in Python.

dealerships = {
    "Atlanta Branch": 1200,
    "Houston Branch": 1100,
    "San Diego Branch": 900,
    "New York Branch": 1500
}

cars_sold_list = []
for num in dealerships.values():
    cars_sold_list.append(num)
print(cars_sold_list)
dictionary values to list python using for loop

In the above code, we have a dictionary named dealerships, where Branch names are included as a key, and the number of cars sold out is its value. So we create an empty list like this: cars_sold_list = [], so we can store all the dictionary values in this list.

Then, use the for loop to iterate over all the values of the dealership dictionary like this:
for num in dealerships.values():” and append them to the car_sold_list like this: cars_sold_list.append(num).

READ:  Python dictionary dict() method [With Examples]

Convert Dict Values to List Python using List Comprehension

Here, we will use list comprehension, which will use the same logic that we’ve used in the previous example, but the difference is that it will use the one-liner for loop, and in the list comprehension, there is no need to append because it will create the list directly.

Let’s understand how to Convert Dict_Values to a list in Python using List comprehension.

landmarks = {
    "Statue of Liberty": 4.5,
    "Grand Canyon": 6.0,
    "Mount Rushmore": 3.0,
    "Golden Gate Bridge": 10.0
}

tourists_list = [num for num in landmarks.values()]
print(tourists_list)
python dictionary values to list in Python using list comprehension in Python

In the above example, we are using a one-liner for loop in the List comprehension, which will iterate over every value using “for num in landmarks.values()” and create a list of all values “tourists_list = [num for num in landmarks.values()]”.

Python Dictionary Values to List using map() Function

We can also use the map() function in Python, which will iterate over every dictionary key and apply the lambda function. So, we will collect all the values from the dictionary using the map() and lambda() methods in Python.

Here’s an example that shows how to convert dict_values to list in Python using the map() function.

newspapers = {
    "The New York Times": "New York",
    "Los Angeles Times": "Los Angeles",
    "The Washington Post": "Washington, D.C.",
    "The Boston Globe": "Boston"
}

newspaper_cities = list(map(lambda x: newspapers[x], newspapers))
print(newspaper_cities)
python dict values to list using map()

In the above code, we have a dictionary of Newspaper names as a key and cities as a value, and we need to extract all the cities from dict to list in Python.

READ:  Python Modulo Operator - What Does the Percent (%) Sign Mean in python

So, we are using the lambda() method to target the value of the key like this
lambda x: newspapers[x]“, and then the map() method will iterate over every key of the dict and apply the lambda function to extract dict values.

Then, we use the list constructor to convert it to a list, such as list(map(lambda x: newspapers[x], newspapers)).

Conclusion

In this Python article, you learned how to convert dict_values to list in Python using different methods, such as the dict.values(), map(), lambda() function, and other approaches, such as using the for loop and List comprehension.

We are using different scenarios in the examples so you will understand where you should use these methods and techniques.

You may like to read: