Reputation: 258
I want to create an if-else condition on dictionaries with missing keys. Such that, if the key is missing then append the value None
, otherwise append the data when it exists.
However, this approach would stop with an error as when the key does not exist it seems to not return True or False
.
For example:
df = [{'data':'test', 'videos':5, 'likes':4}, {'data':'test','likes':4}]
videos = []
for i in df:
if i['videos']:
videos.append(i)
elif i['videos'] is not True:
videos.append('None')
Output:
{'data': 'test', 'videos': 5, 'likes': 4}
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
/var/folders/dr/9wh_z8y10fl79chj86pq7knc0000gn/T/ipykernel_3180/3991749010.py in <module>
1 df = [{'data':'test', 'videos':5, 'likes':4}, {'data':'test','likes':4}]
2 for i in df:
----> 3 if i['videos']:
4 print(i)
5 elif i['videos'] is not True:
KeyError: 'videos'
Upvotes: 1
Views: 1943
Reputation: 9417
TL;DR: Use the defaulting getter dict.get(key, default)
like videos.append(i.get('videos', 'None'))
.
string-indexing a dict entry will always raise KeyError
if the key was not found.
Use the safe dict.get(key)
method which returns None
as default if key is not found.
See also: Why dict.get(key) instead of dict[key]?
Your if-statement could be simplified to:
video = i.get('videos') # use a safe get by key
videos.append(video if video else 'None') # if not found, then default string
The construct x if condition else y
is called conditional expression (ternary operation) - not to confuse with Elvis operator: Does Python have the Elvis operator?
Just for the completeness to catch and handle a KeyError
is also an option:
try:
video = i['videos'] # unsafe may raise KeyError
except KeyError:
video = 'None' # default
videos.append(video) # after the try-except the video is guaranteed to have a string
See the Python tutorial: Try and Except in Python
Upvotes: 0
Reputation: 9417
Ah ... now I understood. You just want to filter the list for items that contain 'videos' ?!
Maybe use the filter
function.
See my demo on interactive Python shell:
>>> df = [{'data':'test', 'videos':5, 'likes':4}, {'data':'test','likes':4}]
>>> list(filter(lambda i: i.get('videos') is not None, df))
[{'data': 'test', 'videos': 5, 'likes': 4}]
Note: the return of filter
is a filter-object which needs to be converted using list()
.
Upvotes: 0
Reputation: 11002
I would go for a list comprehension using .get()
method of Python's dictionaries as this one would return None
by default if the requested key is missing:
df = [{'data':'test', 'videos':5, 'likes':4}, {'data':'test','likes':4}]
videos = [e.get("videos") for e in df]
Upvotes: 2
Reputation: 6090
Just check if the key is actually in dict.keys()
df = [{'data':'test', 'videos':5, 'likes':4}, {'data':'test','likes':4}]
videos = []
for i in df:
if 'video' in i.keys():
videos.append(i)
else:
videos.append('None')
Upvotes: 1