Home > Back-end >  Delete a nested dictionary from a list of dictionary on a condition?
Delete a nested dictionary from a list of dictionary on a condition?

Time:11-08

I have the following dictionary with nested dict. I want to remove the nested dictionary if the key (object-name) has apple in it else keep it.

Input

{
    "item": [
        {
            "seq_key": [
                {
                    "height": 153,
                    "object-name": "person:1"
                },
                {
                    "height": 107,
                    "object-name": "apple:1"
                },
                {
                    "height": 90,
                    "object-name": "orange:2"
                },
                {
                    "height": 84,
                    "object-name": "apple:3"
                },
                {
                    "height": 94,
                    "object-name": "apple:4"
                }
            ],
            "f-no": "0"
        },
        {
            "seq_key": [
                {
                    "height": 161,
                    "object-name": "person:1"
                },
                {
                    "height": 84,
                    "object-name": "mango:3"
                }
            ],
            "f-no": "1"
        }
    ]
}

My Expected Output

{
    "item": [
        {
            "seq_key": [
                {
                    "height": 153,
                    "object-name": "person:1"
                },
                {
                    "height": 90,
                    "object-name": "orange:2"
                }
            ],
            "f-no": "0"
        },
        {
            "seq_key": [
                {
                    "height": 161,
                    "object-name": "person:1"
                },
                {
                    "height": 84,
                    "object-name": "mango:3"
                }
            ],
            "f-no": "1"
        }
    ]
}

What i tried

import json 

with open(r"my_file.json", encoding='utf-8') as f:
    abc = json.load(f)

for key,value in abc.items():
    for i in value:
        for n,item in enumerate(i.get('seq_key')):
            if item.get('object-name').split(":")[0]  == 'apple':
                item.clear()

The above code gives empty dict ,I am not sure how to remove from the list. If i try to use list removal method only two items gets removed not all.

can anyone help me , how to remove it?

CodePudding user response:

Instead of trying to remove list elements, you can use a list comprehension to create a new list with the elements you are interested in:

for seq in abc["item"]:
    seq["seq_key"] = [d for d in seq["seq_key"] if "apple" not in d["object-name"]]

CodePudding user response:

I think your code is mostly right, except that instead of trying to clear the item you can probably pop that item off the list -

for key,value in abc.items():
    for i in value:
        for n,item in enumerate(i.get('seq_key')):
            if item.get('object-name').split(":")[0]  == 'apple':
                i.get('seq_key').pop(n)
  • Related