Remove dictionary from list

前端 未结 7 1217
醉酒成梦
醉酒成梦 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:35

    You can try the following:

    a = [{'id': 1, 'name': 'paul'},
         {'id': 2, 'name': 'john'}]
    
    for e in range(len(a) - 1, -1, -1):
        if a[e]['id'] == 2:
            a.pop(e)
    

    If You can't pop from the beginning - pop from the end, it won't ruin the for loop.

提交回复
热议问题