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?