Python Create List from Dict Values (5 Best Ways)

In Python, transforming the values from a dictionary into a list is a common operation, especially when you need to perform list-specific operations or simply require an ordered collection of dictionary values.

Problem Formulation

Suppose you have a dictionary:

pet_counts = {"Dogs": 10, "Cats": 5, "Parrots": 2}

You want to create a list of the counts, resulting in:

[10, 5, 2]

Let’s explore several methods to achieve this transformation.

Method 1: Using dict.values() with List Comprehension

Get the values from the dictionary using .values() method and create a list from them using list comprehension.

Here’s an example:

pet_counts = {"Dogs": 10, "Cats": 5, "Parrots": 2}
values_list = [value for value in pet_counts.values()]
print(values_list)

The list comprehension iterates over the collection of values returned by pet_counts.values(), creating a new list that consists of these values.

However, I find the following approach even more Pythonic:

Method 2: Using dict.values() with the list() Constructor

Directly convert the collection of dictionary values to a list using the list() constructor.

Here’s an example:

pet_counts = {"Dogs": 10, "Cats": 5, "Parrots": 2}
values_list = list(pet_counts.values())
print(values_list)

pet_counts.values() provides a view object that is passed to list(), which then constructs a list out of this view.

Method 3: Using map() Function

Utilize map() function to iterate over the values and return them in a list format.

Here’s an example:

pet_counts = {"Dogs": 10, "Cats": 5, "Parrots": 2}
values_list = list(map(lambda x: x, pet_counts.values()))
print(values_list)

map() is a higher-order function that applies a given function (in this case, a lambda function that returns what it receives) to all items in an iterable.

Method 4: Using List Extension in a Loop

This manual method iteratively extends a list with the values in the dictionary.

Here’s an example:

pet_counts = {"Dogs": 10, "Cats": 5, "Parrots": 2}
values_list = []
for value in pet_counts.values():
    values_list.append(value)
print(values_list)

The for loop goes through each value in the dictionary and appends it to values_list using append().

Method 5: Using a Generator Expression with list()

Use a generator expression to lazily evaluate expression and generate values which are passed to list().

Here’s an example:

pet_counts = {"Dogs": 10, "Cats": 5, "Parrots": 2}
values_list = list(value for value in pet_counts.values())
print(values_list)

A generator expression is similar to list comprehension but it doesn’t create a list in memory. Instead it creates a generator, which is then consumed by the list() constructor to form a list.

Bonus One-Liner Method 6: Unpacking with the * Operator

You can unpack dictionary values directly into a list using the asterisk * operator.

Here’s an example:

pet_counts = {"Dogs": 10, "Cats": 5, "Parrots": 2}
values_list = [*pet_counts.values()]
print(values_list)

The * operator unpacks the values object allowing direct conversion to a list.

Summary/Discussion

In Python, converting dictionary values into a list can be straightforward using various methods such as

  • using list comprehension with dict.values(),
  • simply wrapping the values object with list(),
  • applying the map() function,
  • extending lists in a loop, creating a list from a generator expression, or
  • unpacking with the * operator.

Feel free to check out our related tutorials on this topic:

πŸ’‘ Recommended Reads: