This is the result of mutating a data structure during iteration. The for
loop essentially creates an iterator over your list, with each item calling next(iterator)
. However, popping items off changes what the iterator is looking at
a = [1, 2, 3, 4]
it = iter(a)
# First iteration
next(it)
1
# remove element
a.pop(0)
1
# we skipped 2!
next(it)
3
Why? Well, we effectively changed what element the iterator is pointing to by removing the element we were currently on. We were looking at the first element in the sequence, but that was removed, so now the second element is the first one. So the call to next
then points to the following element. This winds up looking like it was skipped when it wasn't, you just unintentionally had elements shuffled forward.
To avoid this, it's best to create a new list by filtering, as @AlexanderLekontsev suggests in his answer. This avoids mutating while iterating. You can do this with a standard loop with append like so:
newlist = []
for num in cl:
if num >= d:
newlist.append(num)