简体   繁体   中英

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?

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"]]

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM