Get a list of values from a list of dictionaries?

Using a simple list comprehension (if you’re sure every dictionary has the key):

In [10]: [d['key'] for d in l]
Out[10]: [1, 2, 3]

Otherwise you’ll need to check for existence first:

In [11]: [d['key'] for d in l if 'key' in d]
Out[11]: [1, 2, 3]

Leave a Comment