Sometimes, we want to get a list of values from a list of dicts with Python.
In this article, we’ll look at how to get a list of values from a list of dicts with Python.
How to get a list of values from a list of dicts with Python?
To get a list of values from a list of dicts with Python, we can use list comprehension.
For instance, we write
[d['value'] for d in l if 'value' in d]
to get the value of each dict d
with the key 'value'
from the dicts in list l
.
We use if 'value' in d
to ensure that the key 'value'
is in the dict before we try to get the value.
Conclusion
To get a list of values from a list of dicts with Python, we can use list comprehension.