I have a dictonary that looks something like this:
{
'key1':
{
'a': 'key1',
'b': 'val1',
'c': 'val2'
},
'key2':
{
'a': 'key2',
'b': 'val3',
'c': 'val4'
},
'key3':
{
'a': 'key3',
'b': 'val5',
'c': 'val6'
}
}
I trying to delete the elements in the nested dict based on the key "a" to get an output like this:
{
'key1':
{
'b': 'val1',
'c': 'val2'
},
'key2':
{
'b': 'val3',
'c': 'val4'
},
'key3':
{
'b': 'val5',
'c': 'val6'
}
}
I wrote the following snippet for it:
for k in dict_to_be_deleted:
del k["a"]
I keep getting Key Error: k not found. I tried the following method as well:
for i in dict_to_be_deleted:
for k,v in i.items():
if "a" in k:
del i[k]
I get
Attribute Error: str object has no attribute items
But isn't it suppose to be a dictionary since dict_to_be_deleted is a nested dictionary? I am pretty confused with this. I greatly appreciate any pointers in this regard.