python - Checking if nested attribute exists -
i have nested ordereddict want extract value out of. before can extract value have make sure long chain of attributes exist , values aren't none.
what pythonic way of improving following code:
if 'first' in data , \ data['first'] , \ 'second' in data['first'] , \ data['first']['second'] , \ 'third' in data['first']['second'] , \ data['first']['second']['third']: x = data['first']['second']['third']
another route use get() method:
x = data.get('first', {}).get('second', {}).get('third', none) if @ point key not exist, x = none
Comments
Post a Comment