jxn jxn - 1 month ago 7
Python Question

python iterate over a list to remove index of item

i have 2 lists:

l1=[(1,2),(3,4),(5,6)]
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:
l2.pop(i)


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
l2 = set(l2)   # for speedy lookups
l1 = [x for i, x in enumerate(l1) if i not in l2]
Comments