0

I was doing some stuff in Python, and i faced a strange behaviour of a for loop. What I was trying to do is to delete an element of a list when a certain condition is met:

for l, line in enumerate(lines):
  temp = line.split()
  if '_' not in temp[0]:
    del lines[l]

But, when I execute this code, the list named lines still contains words without the underscore on the first element of a line. So, I tried to reiterate the same code through checking the length of lines before and after the execution of the code:

temp1 = 1
temp2 = 0
while temp1 != temp2:
  temp1 = len(lines)
  for l, line in enumerate(lines):
    temp = line.split()
    if '_' not in temp[0]:
      del lines[l]
  temp2 = len(lines)
  print(temp1,temp2)

And what i get in output is confirming that this for loop requires more than an iteration to be completed:

82024 57042
57042 44880
44880 38908
38908 36000
36000 34611
34611 33937
33937 33612
33612 33454
33454 33378
33378 33343
33343 33327
33327 33320
33320 33317
33317 33315
33315 33315

Anyone can explain why?

mao95
  • 1,046
  • 12
  • 21
  • 1
    Don't remove elements from a list while iterating over it. If you need to, see https://stackoverflow.com/questions/6022764/python-removing-list-element-while-iterating-over-list?noredirect=1&lq=1 – Ignatius Jul 06 '19 at 09:12

1 Answers1

1

You should, in general, never change something you are iterating over. You want to change your code so that you create a new list instead of deleting items.

new_temp = []
for l, line in enumerate(lines):
  temp = line.split()
  if '_' in temp[0]:
    new_temp.append(lines[l])
Akaisteph7
  • 5,034
  • 2
  • 20
  • 43