Python - How To Deal With Missing Keys While Iterating Through A List Of Dictionaries?
In the following example I iterate through a list of dictionaries and I save the 'age' on a list. However, the second dictionary does not have the key 'age'. In that case I would l
Solution 1:
You can use dict.get(key, default_value)
method, If the key not exist it will return default value. If you don't set default value it will return None
ages = [li.get('age', 0) for li in my_list]
Post a Comment for "Python - How To Deal With Missing Keys While Iterating Through A List Of Dictionaries?"