Convert Dictionary Values to List in Python

In this Python tutorial, I will explain How to Convert Dictionary Values to List in Python using different methods in Python with illustrative examples. In the process in we will also see what is dictionary and list in Python and some reasons why we need to convert dictionary values to list in Python and some points for consideration.

When working with Python, one often comes across various data structures. Among them, dictionaries and lists are two of the most frequently used. Sometimes, we need to convert dictionary values to list in Python.

Understanding Python Dictionary and List

Before we jump into how to convert dictionary values to list, it’s essential to grasp the basic understanding of these two data structures in Python.

Python Dictionary (dict): It’s an unordered collection of data in a key:value pair form. Here’s an example:

state_capitals = { "California": "Sacramento", "Texas": "Austin", "Florida": "Tallahassee" }
print(type(state_capitals))

Output:

<class 'dict'>
Dictionary in Python

Python List: It’s an ordered collection of items that can be of any type. Lists are mutable, meaning they can be modified after creation.

example_list = ["California", "Texas", "Florida"]
print(type(example_list))

Output:

<class 'list'>
list in Python

Why Convert Dictionary Values to List in Python?

Often, when we’re dealing with operations like sorting, filtering, or any other form of list-based manipulation, we might want to extract and work solely with the dictionary values. For such cases, it becomes imperative to know how to convert dictionary values to list in Python.

How to Convert Dictionary Values to List in Python

There are four different approaches to convert dict values into a list in Python.

  • The direct approaches:
    • Using dict.values() Method
    • Using a For Loop
  • Advanced Methods
    • Using List Comprehension
    • Using map() function

Let’s see them one by one with some demonstrative examples:

Method-1: Convert dict values to a list in Python using dict.values() method

The simplest method to convert dictionary values to list Python offers is using the values() method of the dictionary followed by the list() function.

The values() method in Python dictionary is a built-in method that provides a way to extract all the values from a dictionary. This method returns a ‘dict_values‘ object, which can then be converted to a list.

While, the list() method in Python is a built-in function that creates a list from an iterable (like a tuple, string, list, or dictionary).

For instance, We work for a research institute that studies annual average temperatures in various US cities. We want to analyze the data, but first, we need to separate the temperatures from the stored data in the form of a Python dict.

temperatures = {
    "Miami": 77.0,
    "Denver": 50.2,
    "Seattle": 52.0,
    "Los Angeles": 64.3
}

avg_temps_list = list(temperatures.values())
print(avg_temps_list)

The output is:

[77.0, 50.2, 52.0, 64.3]
python dictionary values to list using dict.values

In the above code, temperatures.values() retrieves the dictionary values, and list() converts these dict values to list Python.

Method-2: Convert values of dictionary to list Python using for loop

For those who prefer a traditional approach, a standard for loop can be utilized. The for loop in Python is used to iterate over a sequence (like a list, tuple, or string) or other iterable objects.

For example, We’re a manager at a car dealership network across the US. We have a Python dictionary showing the number of cars sold in various branches in the past year. For an annual review, we need to compile these numbers as a Python list.

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)

The output is: Here, we will create an empty Python list(cars_sold_list) and for each value in the dictionary we will append the value in that list and finally, we get,

python convert dict values to list using for loop

The for-loop in the Python method iteratively adds each dictionary value to list.

Method-3: Convert dictionary values to list Python using list comprehension

While the direct approach works well, there are other methods to store dictionary values in list Python, one of them is list comprehension.

List Comprehension is a concise way to convert dictionary values to list Python facilitates:

For instance, We work for a tourism agency and we have data as a Python dict, on the number of tourists visiting major US landmarks annually. To prepare a report, we wish to compile just the numbers in a Python list.

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)

The output is:

[4.5, 6.0, 3.0, 10.0]
convert dict values to list python using list comprehension

List comprehension provides a concise way to store dictionary values in list Python,

Method-4: How to store dictionary values in list in Python using the map() function

The map() function is a functional approach to convert dict values to list. This function applies a specified function to each item in an input list (or any iterable).

For example, We’re exploring major USA newspapers and the cities they are primarily associated with.

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)

The output is:

['New York', 'Los Angeles', 'Washington, D.C.', 'Boston']
how to convert dict values to list in python using map function

With the map() function, a lambda function captures each newspaper’s primary city, generating the list in Python.

Tips and Considerations for converting dictionary values to list Python

  • Memory Usage: If we have a large dictionary, converting dictionary values to list might consume more memory. Always be cautious and ensure we have adequate resources.
  • Preserving Order: Since Python 3.7, dictionaries maintain the order of their elements. When converting dict values to list python maintains this order by default.
  • Duplicate Values: If a dictionary has duplicate values and we convert them into a list, these duplicates will be preserved in the resultant list.

Conclusion

In this tutorial, we have seen how to convert Dictionary Values to List in Python using four different methods in Python like dict.values, for loop, list comprehension, or map function with illustrative examples.

Whether we want to convert values of dictionary to list Python for data manipulation, sorting, or any other reason, it’s a simple task with the above methods. The versatility of Python’s data structures and the plethora of built-in functions make tasks like dict.values to list conversion straightforward.

You may also like to read: