I am looking for a convenient, safe python dictionary key access approach. Here are 3 ways came to my mind.
data = {'color': 'yellow'}
# approach one
color_1 = None
if 'color' in data:
    color_1 = data['color']
# approach two
color_2 = data['color'] if 'color' in data else None
# approach three
def safe(obj, key):
    if key in obj:
        return obj[key]
    else:
        return None
color_3 = safe(data, 'color')
#output
print("{},{},{}".format(color_1, color_2, color_3))
All three methods work, of-course. But is there any simple out of the box  way to achieve this without having to use excess ifs or custom functions?
I believe there should be, because this is a very common usage.
 
     
     
    