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

Popular posts from this blog

shopping cart - Page redirect not working PHP -

php - How to modify a menu to show sub-menus -

python - Installing PyDev in eclipse is failed -