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)
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)
Advertisement
Answer
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)