Removing key-based elements in nested dictionaries in python

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_deletedis a nested dictionary? I am pretty confused about this. I greatly appreciate any guidance in this regard.

+4
source share
5 answers

, dict_to_be_deleted, . Attribute Error , i - , . , .values(), .

for v in dict_to_be_deleted.values():
    del v["a"]

, , , Ajax . , , , - .

+4

:

d = {
'key1': 
    {
        'a': 'key1', 
        'b': 'val1', 
        'c': 'val2'
    }, 
 'key2': 
    {
        'a': 'key2', 
        'b': 'val3', 
        'c': 'val4'
    }, 
'key3': 
    {
        'a': 'key3', 
        'b': 'val5', 
        'c': 'val6'
    }
 }
new_d = {a:{c:d for c, d in b.items() if c != 'a'} for a, b in d.items()}

:

{'key3': {'c': 'val6', 'b': 'val5'}, 'key2': {'c': 'val4', 'b': 'val3'}, 'key1': {'c': 'val2', 'b': 'val1'}}
+3

dict.pop():

data = {
        'key1': 
            {
            'a': 'key1', 
            'b': 'val1', 
            'c': 'val2'
            }, 
        'key2': 
            {
            'a': 'key2', 
            'b': 'val3', 
            'c': 'val4'
            }, 
        'key3': 
            {  
            'a': 'key3', 
            'b': 'val5', 
            'c': 'val6'
            }
        }

for key in data:
    data[key].pop('a', None)

print(data)

:

{'key1': {'b': 'val1', 'c': 'val2'}, 'key2': {'b': 'val3', 'c': 'val4'}, 'key3': {'b': 'val5', 'c': 'val6'}}

dict.pop() , , , "a", . , None, KeyError.

+3

pop() itervalues ​​()

[ .pop('a', None) d.itervalues ​​()]

d

output > {'key3': {'c': 'val6', 'b': 'val5'}, 'key2': {'c': 'val4', 'b ':' val3 '},' key1 ': {' c ':' val2 ',' b ':' val1 '}}

Advantages: it does not take up extra memory. because we are not creating a new dict here

and if you are looking for simplicity @ The answer to Ajax1234 is more descriptive

+1
source

Why are you deleting when you can retrieve what you need? Here is the collection.defaultdict (dict) method:

data={
    'key1':
        {
            'a': 'key1',
            'b': 'val1',
            'c': 'val2'
        },
    'key2':
        {
            'a': 'key2',
            'b': 'val3',
            'c': 'val4'
        },
    'key3':
        {
            'a': 'key3',
            'b': 'val5',
            'c': 'val6'
        }
}

import collections

dict_1=collections.defaultdict(dict)
for key,value in data.items():
    for key_1,value_1 in value.items():
        if key!='a':
            dict_1[key][key_1]=value_1

print(dict_1)

output:

{
  "key1": {
    "b": "val1",
    "c": "val2"
  },
  "key2": {
    "b": "val3",
    "c": "val4"
  },
  "key3": {
    "b": "val5",
    "c": "val6"
  }
}
0
source

Source: https://habr.com/ru/post/1691489/


All Articles