You can use dict.get()
to default to an empty list if the key is missing:
b = [i for i in mydict.get('entries', [])]
In your version, the if
filter only applies to each iteration as if you nested an if
statement under the for
loop:
for i in mydict['entries']:
if mydict['entries']:
which isn't much use if entries
throws a KeyError
.