Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to remove an item from every dict in a list

Sample:

sample = {
    "value1": "foo",
    "value2": "bar",
    "value3": "baz",
    "array": [
        {
            "key1":'value1',
            "key2":'value2',
            "key3":'value3',
        },
        {
            "key1":'value4',
            "key2":'value5',
            "key3":'value6',
        },
        {
            "key1":'value7',
            "key2":'value8',
            "key3":'value9',
        }
          ]
}

I need to delete all key1s and values from all dicts in this array leaving only key2, key3 in each object.

The only thing I can find from google is how to iterate and delete an entire dict in an list, not a single key.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Haven’t tried deleting but I did just try to get the other values like this.. couldn’t get this either

    domains_list = dict(map(lambda item: (item['key1'], item['key2']), sample['array'].items()))

error: AttributeError: ‘list’ object has no attribute ‘items’

>Solution :

To get You moving: sample['array'] is a list (python type), which doesnt have .items(). So I’d iterate something like for dictionary in sample['array'] and then call dictionary.items().

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading