Python Safely Navigate a Complex Data Structure: Difference between revisions
Jump to navigation
Jump to search
Line 7: | Line 7: | ||
<syntaxhighlight lang='py'> | <syntaxhighlight lang='py'> | ||
yaml = ... | yaml = ... | ||
v = None | |||
first_level_map = yaml.get('first_level_key', {}) | |||
if first_level_map: | |||
second_level_map = first_level_map.get('second_level_key', {}) | |||
if second_level_map: | |||
v = second_level_map.get('leaf_key') | |||
return v | |||
</syntaxhighlight> | </syntaxhighlight> |
Revision as of 00:16, 3 March 2022
Internal
Overview
yaml = ...
v = None
first_level_map = yaml.get('first_level_key', {})
if first_level_map:
second_level_map = first_level_map.get('second_level_key', {})
if second_level_map:
v = second_level_map.get('leaf_key')
return v