Sometimes, we want to get list of values from a Python dict.
In this article, we’ll look at how to get list of values from a Python dict.
How to get list of values from a Python dict?
To get list of values from a Python dict, we can use the dict’s values
method and the list
function.
For instance, we write:
d = {'a': 1, 'b': 2}
v = list(d.values())
print(v)
We call d.values
to return a view of the values.
Then we use the list
function to convert the values view into a list.
Therefore, v
is [1, 2]
.
Conclusion
To get list of values from a Python dict, we can use the dict’s values
method and the list
function.