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
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)