-2

I have the for-loop:

L=[1,2,3,4]

I want, that the code below works:

for i in range(1,len(L)):
   if i%2==0:
      L.remove(L[i])

How can I do this? Many thanks.

veingrow
  • 15
  • 6

1 Answers1

0

So you want even indexed elements? Comprehensions are your friend.

L = [e for i,e in enumerate(L) if i % 2]

Tyler Sebastian
  • 9,067
  • 6
  • 39
  • 62