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

Deleting multiple elements in a list – with a list of item locations

I have two lists.

List1 is the list of items I am trying to format

List2 is a list of item locations in List1 that I need to remove (condensing duplicates)

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

The issue seems to be that it first removes the first location (9) and then removes the second (16) after…instead of doing them simultaneously. After it removes 9, the list is changed and 16 is removed in a different intended location because of that.

List1 = ["HST", "BA", "CRM", "QQQ", "IYR", "TDG", "HD", "TDY", "UAL", "CRM", "XOM", "CCL", "LLY", "QCOM", "UPS", "MPW", "CCL", "ILMN", "MU", "GOOGL", "AXP", "IVZ", "WY"]
List2 = [9, 16]

print(List1)
print(List2)


for x in List2:
    List1.pop(x)

print(List1)

>Solution :

try with something like that

List1 = ["HST", "BA", "CRM", "QQQ", "IYR", "TDG", "HD", "TDY", "UAL", "CRM", "XOM", "CCL", "LLY", "QCOM", "UPS", "MPW", "CCL", "ILMN", "MU", "GOOGL", "AXP", "IVZ", "WY"]
List2 = [9, 16]

print(List1)
print(List2)


for i, x in enumerate(List2):
    List1.pop(x-i)

print(List1)
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