I am trying to delete a dictionary item nested within a list, in a dict.
After I del
the object, the modified dictionary will contain an empty dictionary: {}
in the place of the deleted object.
Here is the code I am using:
json_dict = {"top_key": "top_value", "bottom_key": [{"list_dict": "list_dict_value"},{"list_dict1": "list_dict_value1"}]}
print("initial:", json_dict)
def delete(list_dict):
for i in json_dict["bottom_key"]:
if list_dict in i:
del i[list_dict]
delete("list_dict")
print("final:", json_dict)
The final print will return:
final: {'bottom_key': [{}, {'list_dict1': 'list_dict_value1'}], 'top_key': 'top_value'}
(pw-retriever) simon@[pw-retriever](fix_del_ls)
I am trying to find a way to remove the {}
in addition to the key:value pair, in one go.
edit: an explanation as to what the heck is going on would also be highly appreciated.