Monday, 3 October 2016

list - Python: Deeply nested dictionary editing




I have the following situation:



data = {'key1' : 'value1',

'key2': [
{'subkey1': 'subvalue1',
'subkey2': 'subvalue2'},

{other dictionaries}
...
]
}



I have the exact dictionary:



{'subkey1': 'subvalue1', 'subkey2': 'subvalue2'}


stored in a variable. I would like to remove it from the data dict. How could I do this?


Answer



lists have a remove method, so if you know the key



data['key2'].remove({'subkey1': 'subvalue1', 'subkey2': 'subvalue2'})



No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...