简体   繁体   中英

Remove multiple dictionary value from a list using Python

My data is as follow

dd=[{'id':'aa','age':22,'data':{},'background':{}},
      {'id':'bb','age':23,'data':{},'background':{}},
      {'id':'cc','age':24,'data':{},'background':{}},
      {'id':'dd','age':25,'data':{},'background':{}},
      {'id':'ee','age':26,'data':{},'background':{}}      
     ]

How to remove several responses based on id? I have almost 100 responses that need to be removed.

As example:

id = ' aa bb cc '

Use list comprehension to filter out the data you do not want. However, you should not use the name id

dd = [item for item in dd if item['id'] not in id]

您还可以在此处使用filterlambda函数,

dd = list(filter(lambda x : x["id"] not in a, dd))

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