How to Convert Dict_Values to List in Python

When I first started working with Python dictionaries more than a decade ago, I often ran into a common situation. I would use the values() method to access dictionary values and get something called dict_values.

At first glance, this dict_values object looks like a list. But when I tried to perform list operations such as indexing or slicing, it didn’t behave like a regular list.

Over the years, I discovered several simple and reliable ways to convert dict_values into a proper Python list. In this tutorial, I’ll share those methods with you, step by step, so you can quickly apply them in your own projects.

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 the 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

Method 1: Use Python’s 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.

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 a list.

Here’s an example that shows how to convert dict_values to a 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)

I executed the above example code and added the screenshot below.

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.

Method 2: Use a 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)

I executed the above example code and added the screenshot below.

dictionary values to list python using for loop

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

Method 3: Use 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 a one-liner for loop, and in the list comprehension, there is no need to append because it will create the list directly.

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)

I executed the above example code and added the screenshot below.

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()]”.

Method 4: Use 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.

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)

I executed the above example code and added the screenshot below.

python dict values to list using map()

In the above code, we have a dictionary of Newspaper names as key and cities as a values, and we need to extract all the cities from the dictionary to a list 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.

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:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.