5 Best Ways to Convert Python Dict to List of Values

πŸ’‘ Problem Formulation: Python dictionaries are unordered collections of items. While working with dictionaries, you might need to extract all values as a list for further processing or iteration. Say you have a dictionary {'a': 1, 'b': 2, 'c': 3} and want to convert it to the list of values [1, 2, 3]. This article demonstrates how to accomplish this task using various methods in Python.

Method 1: Using List Comprehension

List comprehension in Python is a compact way of creating lists. The syntax is expressive and highly readable. To extract values from a dictionary, you can use a list comprehension that iterates over the dictionary’s values. This is efficient and recommended for simple cases.

Here’s an example:

my_dict = {'apple': 1, 'banana': 2, 'cherry': 3}
values = [value for value in my_dict.values()]
print(values)

Output:

[1, 2, 3]

In this example, the list comprehension [value for value in my_dict.values()] iterates through each value in the dictionary’s .values() view and creates a new list of these values.

Method 2: Using the .values() Method

The .values() method returns a view object that displays a list of all the values in the dictionary. You can convert this view into a list using the list() constructor. This is Pythonic and works best for direct conversions without additional processing.

Here’s an example:

my_dict = {'apple': 1, 'banana': 2, 'cherry': 3}
values = list(my_dict.values())
print(values)

Output:

[1, 2, 3]

In the snippet, list(my_dict.values()) directly converts the values view into a list. This method is concise and easily readable.

Method 3: Using the map() Function

The map() function applies a given function to all items in an iterable and returns a map object. By passing the dict.values and the list constructor to map(), you can convert a dictionary’s values to a list in one go. This method is suitable when you need to apply further transformations to dictionary values.

Here’s an example:

my_dict = {'apple': 1, 'banana': 2, 'cherry': 3}
values = list(map(lambda x: x, my_dict.values()))
print(values)

Output:

[1, 2, 3]

This code applies a lambda function that returns its argument (effectively doing nothing in this case) to each value in the dictionary, then the list() constructor creates a list from the map object.

Method 4: Using itertools.chain()

If you have multiple dictionaries and want to combine their values into a single list, the itertools.chain() function from Python’s itertools module can be used. It creates an iterator that aggregates elements from each of the iterables.

Here’s an example:

import itertools

dicts = [{'apple': 1}, {'banana': 2}, {'cherry': 3}]
values = list(itertools.chain(*[d.values() for d in dicts]))
print(values)

Output:

[1, 2, 3]

This example uses list comprehension to create a list of value views from each dictionary, and then itertools.chain(*iterables) combines these views into one iterable, which is then converted to a list.

Bonus One-Liner Method 5: Using sum() Function

While not as intuitive, the sum() function can be used to concatenate lists by providing an empty list as the start value. This trick is great for combining values from multiple dictionaries into one flat list.

Here’s an example:

dicts = [{'apple': 1}, {'banana': 2}, {'cherry': 3}]
values = sum((d.values() for d in dicts), [])
print(values)

Output:

[1, 2, 3]

The expression sum((d.values() for d in dicts), []) concatenates all the values from the generator expression into a single list.

Summary/Discussion

  • Method 1: List Comprehension. Fast and Pythonic. Best for simple cases where you need to convert values into a list without additional processing.
  • Method 2: Using .values(). Readable and straightforward. Recommended for cases when you need a direct conversion. Doesn’t allow for any processing while converting.
  • Method 3: Using map() Function. Versatile and functional. Suitable when transformation of values is required during the list creation. It is less readable for beginners compared to list comprehension.
  • Method 4: Using itertools.chain(). Efficient for combining values from multiple dictionaries. Adds dependency on itertools and is less straightforward compared to the first two methods.
  • Bonus Method 5: Using sum() Function. Interesting one-liner. Non-intuitive use of sum() for list concatenation but can be a succinct solution for combining lists from multiple dictionaries.