Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

remove specific dictionaries in a list Python

I have a list of dictionaries:

persons = [{'id': 1, 'name': 'john'},
           {'id': 2, 'name': 'doe'},
           {'id': 3, 'name': 'paul'}]

ids = [1,3]

# remove_persons(persons, ids) --> [{'id': 2, 'name': 'doe'}]

I would like to remove dictionaries from a list of id.
What is the most efficient way to go about this programmatically.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

I think this will do the jobs

persons = [{'id': 1, 'name': 'john'},
           {'id': 2, 'name': 'doe'},
           {'id': 3, 'name': 'paul'}]

ids = [1, 3]

persons = [person for person in persons if person['id'] not in ids]

print(persons)

print(persons)

but i dont know about the performance

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading