Traverse Nested Dictionary From Depth And Move To Top
I have below dictionary (generated from a report, so the structure can change). I need to go to the depth of the dictionary, find the id which in this case is 'id': u'ef3c8cf1-0987
Solution 1:
Here is the solution I propose:
def remove_id(data):
if isinstance(data, List):
return [remove_id(sub_data) for sub_data indata]
if isinstance(data, Dict):
return {key: remove_id(value) for key, value indata.items()
if key != 'id'}
returndata
And the result:
{'snapshot_id': None,
'snapshots': [{'volumes': [{'snapshot_id': '3ddc7ddd-02ca-4669-a0cb-fb0d56a4a6f5',
'snapshots': []},
{'snapshot_id': '3ddc7ddd-02ca-4669-a0cb-fb0d56a4a6f5',
'snapshots': [{'volumes': [{'snapshot_id': 'd637f6ea-4a41-448c-874f-ffe624ddc597',
'snapshots': []}]}]},
{'snapshot_id': '3ddc7ddd-02ca-4669-a0cb-fb0d56a4a6f5',
'snapshots': []}]}]}
Post a Comment for "Traverse Nested Dictionary From Depth And Move To Top"