ignore keyError in list list comprehension

Viewed 2690

I'm trying to extract all objects that have certain keys in a dict however some dicts do not contain all of the keys and I would like to ignore the keyError and keep going. I`ve seen some implementations of doing this with try and except but it will not work in my case

allValues = []
for dictionary in masterDict:
    values = [(dictionary[x]) for x in keysArray]
    allValues.append(values)
1 Answers
Related