Skip to content Skip to sidebar Skip to footer

How To Remove Elements From One List If Other List Contain The Indexes Of The Elements To Be Removed

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_lis

Solution 1:

You may try following.

for x insorted(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 insorted(set([y for y in listb if -1 < y < len(lista)]),reverse=True): lista.pop(x)

Solution 2:

Post a Comment for "How To Remove Elements From One List If Other List Contain The Indexes Of The Elements To Be Removed"