1

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'
2

4 Answers 4

2

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]
3
  • Output [5, None] as expected?
    – hc_dev
    Commented Feb 16, 2022 at 1:42
  • @hc_dev yes, indeed.
    – daniel451
    Commented Feb 16, 2022 at 1:53
  • I don't think this is the desired output.
    – Wups
    Commented Feb 16, 2022 at 8:42
1

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')
1
  • This works great! a few issues with the code output but I managed to reconfigure it to my needs. Thanks! Commented Feb 16, 2022 at 0:46
0

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().

0

TL;DR: Use the defaulting getter dict.get(key, default) like videos.append(i.get('videos', 'None')).

Lookup by key in a dict

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]?

Make your code simpler and robust

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

1
  • @KellyBundy Thanks for proof-reading and the clarification to learn. Added links and fixed it.
    – hc_dev
    Commented Feb 16, 2022 at 7:46

Not the answer you're looking for? Browse other questions tagged or ask your own question.