Reputation: 1
I have two lists - lista = [1,2,3,5,0,5,6,0] listb = [4,7]
listb contains index numbers. How can I remove index 4 and 7(contained in lisb) from lista.
I thus want to print new_lista as [1,2,3,5,5,6]
I hope it makes sense.
Alwina
Upvotes: 0
Views: 70
Reputation: 3705
You may try following.
for x in sorted(listb,reverse=True): lista.pop(x)
Also you may need to make sure that listb does not contain duplicate index and all the index numbers are valid index.
for x in sorted(set([y for y in listb if -1 < y < len(lista)]),reverse=True): lista.pop(x)
Upvotes: 1
Reputation: 157314
Use enumerate
:
new_lista = [j for i, j in enumerate(lista) if i not in listb]
Upvotes: 0