jxn jxn - 1 year ago 100
Python Question

python iterate over a list to remove index of item

i have 2 lists:

l2 = [0,2]

l2 contains a list of indexes. I want to delete the index of the element in l1 that correspond to the index in l2.

How can that be done?

for i in l2:

doesnt work because the index changes after popping the first one.

i saw this answer somewhere but its not working by deleteing indexes:

somelist = [x for x in somelist if not determine(x)]

Answer Source
l2 = set(l2)   # for speedy lookups
l1 = [x for i, x in enumerate(l1) if i not in l2]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download