'Remove element from list in dictionary

I'm trying to remove element(s) from a list, which is in a dictionary:

data = {"akey": [{"name": "X", "members_id": ["1", "2"]}, {"name": "Y", "members_id": ["1", "3"]}], "bkey": [{"name": "Y", "members_id": ["2", "3"]}]}

print(data)

to_remove = "1"

for key in data:
    for record in data[key]:
        if to_remove in record["members_id"]:
            data[key].remove(record)

print(data)

If to_remove is in record["members_id"], associated item in data[key] should be removed.

In other words, for the example given above, I'm expecting:

{"akey": [], "bkey": [{"name": "Y", "members_id": ["2", "3"]}]}

... but I'm getting

{'akey': [{'name': 'Y', 'members_id': ['1', '3']}], 'bkey': [{'name': 'Y', 'members_id': ['2', '3']}]}

Why is the first item in the list the only one getting removed?



Solution 1:[1]

As you directly iterate on the list for record in data[key], then of you delete one of the item whils iterating that skips the next one, as the iteration is backed by indices, when you deletenone, everybody after move forward once

Iterate on a copy of the list :

for key in data:
    for record in list(data[key]):
        if to_remove in record["members_id"]:
            data[key].remove(record)

Or keep the valid ones, and reassign them

for key in data:
    data[key] = [x for x in data[key] if to_remove not in x["members_id"]]

Solution 2:[2]

The issue is because you try to remove objects inside of a loop. try this

data = {"akey": [{"name": "X", "members_id": ["1", "2"]}, {"name": "Y", "members_id": ["1", "3"]}], "bkey": [{"name": "Y", "members_id": ["2", "3"]}]}

print(data)

to_remove = "1"

for key in data: i = 0

while i < len(data[key]):
    record = data[key][i]
    if to_remove in record["members_id"]:
        data[key].remove(record)
    else:
        i += 1

print(data)

Solution 3:[3]

You need to account for the possibility that the value to_remove may not be in the list you're trying to modify. Therefore:

data = {"akey": [{"name": "X", "members_id": ["1", "2"]}, {"name": "Y", "members_id": ["1", "3"]}], "bkey": [{"name": "Y", "members_id": ["2", "3"]}]}
to_remove = '1'

for d in data['akey']:
    try:
        if isinstance(d, dict):
            d['members_id'].remove(to_remove)
    except (KeyError, ValueError):
        pass

print(data)

Output:

{'akey': [{'name': 'X', 'members_id': ['2']}, {'name': 'Y', 'members_id': ['3']}], 'bkey': [{'name': 'Y', 'members_id': ['2', '3']}]}

Note:

The exception handler allows for the possibility that the dictionary key 'members_id' may be missing as well as the value indicated by to_remove is absent

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 azro
Solution 2 Eyal
Solution 3