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 item not match in list of object list

Here is my list, it’s list of object and inside object there is list:
please rev

[
    {
        "id": 1,
        "test": [
            {
                "id__": 1
            },
            {

                "id__": 1
            },
            {

                "id__": 1
            },
            {
                "id__": 2
            }
        ]
    },
    {
        "id": 2,
        "test": [
            {

                "id__": 1
            },
            {

                "id__": 1
            },
            {

                "id__": 1
            },
            {
                "id__": 2
            }
        ]
    }
]

I want to remove matched id with one in objecso it can be like this :

[
    {
        "id": 1,
        "test": [
            {
                "id__": 1
            },
            {

                "id__": 1
            },
            {

                "id__": 1
            }
        ]
    },
    {
        "id": 2,
        "test": [

            {
                "id__": 2
            }
        ]
    }
]

Here is what I try:
and notice that is final is the list mentioned above

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

for i in final:
    for j in i["test"]:
        if j['id__'] == i["id"]:
            i.pop()

can I use some help of you kind guys, I tried with remove attribute in list, and still no result satisfied.

>Solution :

This is probably what you want:

filtered_result = []
for i in a:
    lst_id = i["id"]
    lst_to_compare = i["test"]
    filtered_inner_list = [item for item in lst_to_compare if item["id__"] == lst_id]
    filtered_result.append({"id": lst_id, "test": filtered_inner_list})

print(filtered_result)
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