I have a dictator 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 am trying to remove elements in a nested dict based on the key "a" to get this output:
{
'key1':
{
'b': 'val1',
'c': 'val2'
},
'key2':
{
'b': 'val3',
'c': 'val4'
},
'key3':
{
'b': 'val5',
'c': 'val6'
}
}
I wrote the following snippet for him:
for k in dict_to_be_deleted:
del k["a"]
I keep getting Key Error: k not found. I also tried the following method:
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 is it not a dictionary, since it dict_to_be_deleted
is a nested dictionary? I am pretty confused about this. I greatly appreciate any guidance in this regard.
source
share