0

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

Alwina
  • 1

2 Answers2

1

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)
shantanoo
  • 3,617
  • 1
  • 24
  • 37
0

Use enumerate:

new_lista = [j for i, j in enumerate(lista) if i not in listb]
ecatmur
  • 152,476
  • 27
  • 293
  • 366