A common task in Python is to extract all the values from a dictionary and store them in a list. This is useful when you no longer need the keys and want to perform list-specific operations like sorting or appending. The primary tool for this is the dict.values() method, which returns a special "view" of the dictionary's values. This view must then be explicitly converted into a list.
This guide will explain the dict.values() object and demonstrate the three standard, Pythonic ways to convert it into a list: using the list() constructor, the unpacking operator (*), and a list comprehension.
Understanding the dict.values() View Object
When you call .values() on a dictionary, it doesn't return a list directly. Instead, it returns a dynamic view object of the type dict_values.
organization = {
'ceo': 'Tom',
'coo': 'Andy',
'cfo': 'Johnny'
}
values_view = organization.values()
print(values_view)
print(type(values_view))Output:
dict_values(['Tom', 'Andy', 'Johnny'])
<class 'dict_values'>This view is a memory-efficient representation that is linked to the original dictionary. If the dictionary changes, the view reflects those changes. However, it is not a list and does not have list-specific methods like .append() or .sort(). To use those, you must convert it.
Method 1: Using the list() Constructor (Recommended)
The most direct, readable, and idiomatic way to convert the dict_values view into a list is to pass it to the built-in list() constructor.
Solution:
organization = {
'ceo': 'Tom',
'coo': 'Andy',
'cfo': 'Johnny'
}
values_list = list(organization.values())
print(values_list)
print(type(values_list))Output:
['Tom', 'Andy', 'Johnny']
<class 'list'>Method 2: Using the Unpacking Operator (*)
A modern and equally concise alternative is to use the unpacking operator (*) inside a list literal ([]). This operator, also known as the "splat" operator, unpacks the elements from the dict_values iterable and places them into the new list.
Solution:
organization = {
'ceo': 'Tom',
'coo': 'Andy',
'cfo': 'Johnny'
}
values_list = [*organization.values()]
print(values_list)Output:
['Tom', 'Andy', 'Johnny']Method 3: Using a List Comprehension
A list comprehension is another powerful way to create the list. While slightly more verbose for a direct conversion, it becomes extremely useful when you need to filter or transform the values as you convert them.
Solution for Direct Conversion
organization = {
'ceo': 'Tom',
'coo': 'Andy',
'cfo': 'Johnny'
}
values_list = [value for value in organization.values()]
print(values_list)Output:
['Tom', 'Andy', 'Johnny']Solution with Transformation (Example)
Here's where list comprehensions shine. Let's get the values and convert them to uppercase in one line.
organization = {
'ceo': 'Tom',
'coo': 'Andy',
'cfo': 'Johnny'
}
uppercase_values_list = [value.upper() for value in organization.values()]
print(uppercase_values_list)Output:
['TOM', 'ANDY', 'JOHNNY']Conclusion
| Method | Best For | Example |
|---|---|---|
list() constructor |
Simplicity and clarity. The standard, recommended way. | list(my_dict.values()) |
Unpacking operator (*) |
A modern, concise alternative to the list() constructor. |
[*my_dict.values()] |
| List comprehension | Transforming or filtering values during the conversion. | [v.upper() for v in my_dict.values()] |
While all three methods effectively convert dictionary values to a list, list(my_dict.values()) is the most direct and widely understood idiom for a simple conversion.