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

Delete multiple of the same item from list

I am wondering how I would delete all items specified from a list, including all duplicates.

Here is a dumbed down version of my code, and it’s only deleting half of the list each time. Wondering mostly why it doesn’t delete all items at first.

blacklist = ['127.0.0.1', '127.0.0.1', '127.0.0.1', '127.0.0.1', '127.0.0.1', '127.0.0.1', '127.0.0.1', '127.0.0.1', '127.0.0.1', '127.0.0.1']
item = '127.0.0.1'

print(blacklist)
for _ in blacklist:
    blacklist.remove(item)
print(blacklist)

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 :

deduped = set(blacklist)
deduped.remove(item)

To remove multiple items

deduped = set(blacklist)

for item in items_to_remove:
    deduped.remove(item)
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