0

I am trying to pop() all the element that less than four by pop one element per time. However I have no idea why my loop stop working in the middle of my list that left a bunch of 1 that didn't pop...

nums =[16, 95, 100, 3, 12, 5, 8, 5, 1, 3, 1, 12, 4, 1, 4, 5, 4, 1, 1, 1, 1, 2, 1, 1, 3, 1, 1, 1, 2, 1, 1, 1, 1, 4, 1, 4, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 2, 1, 1, 1, 2, 1, 1, 2, 1, 2, 1]

for checker in nums:
    if checker < 4:
        nums.pop(nums.index(checker))

print(nums) #[16, 95, 100, 12, 5, 8, 5, 3, 12, 4, 4, 5, 4, 3, 4, 4, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 2, 1, 1, 1, 2, 1, 1, 2, 1, 2, 1]

1 Answers1

-2

This is because you are changing the size of the list in your for loop.

Kyle Swanson
  • 1,308
  • 1
  • 12
  • 20