Remove dictionary from list

前端 未结 7 1226
醉酒成梦
醉酒成梦 2020-12-04 14:32

If I have a list of dictionaries, say:

[{\'id\': 1, \'name\': \'paul\'},
 {\'id\': 2, \'name\': \'john\'}]

and I would like to remove the d

7条回答
  •  青春惊慌失措
    2020-12-04 14:39

    # assume ls contains your list
    for i in range(len(ls)):
        if ls[i]['id'] == 2:
            del ls[i]
            break
    

    Will probably be faster than the list comprehension methods on average because it doesn't traverse the whole list if it finds the item in question early on.

提交回复
热议问题