key value if len and key in key else FOO try key except IndexError Key

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# x = [{'key':'value'}]
#1:
if len(x) and 'key' in x[0]:
b = x[0]['key']
else:
b = FOO
#2:
try:
b = x[0]['key']
except (IndexError, KeyError):
b = FOO