I have a list of dictionaries that do not necessarily have the same keys. I am trying to make a new list of the values contained in each dictionary that has a specific key.
E.g.
dict1 = {'key1': 1, 'key2': 2, 'key4': 4}
dict2 = {'key2': 2, 'key3': 3}
dict3 = {'key1': 1, 'key2': 2, 'key3': 3}
dict_list = [dict1, dict2, dict3]
Now I'm trying to create a list from dict_list as follows:
key2vals = [dict['key2'] for dict in dict_list]
print(key2vals)
[2, 2, 2]
That works well, since 'key2' is contained in every dictionary in the list. However, when I try a key that does not appear in every dictionary, I get a fault. E.g. 'key1':
>>> key1vals = [dict['key1'] for dict in dict_list]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 1, in <listcomp>
KeyError: 'key1'
Is there a short and straightforward way that I can do something like the following, which does not work?
# This does not work
key1vals = [dict['key1'] if 'key1' in dict for dict in dict_list]
What is the most Pythonic way to accomplish this?
ANSWER: The order of the for and if statements should have been reversed:
key1vals = [dict['key1'] for dict in dict_list if 'key1' in dict ]
Also, with AbrahamB and DJV's suggestions, I was able to add a default value to those dictionaries without the search key:
[dict.get('key1', 0) for dict in dict_list]