Sobes.tech
Junior — Senior

Behavior of removing elements from a list during iteration

livecode

Task condition

Describe how the given code works, which tries to remove all values less than 5 from a list while iterating over the same list.

numbers = [1, 2, 5, 11, 3, 111, 7, 27]

for i in numbers:
    if i < 5:
        numbers.remove(i)

print(numbers)
# Result: [2, 5, 11, 111, 7, 27]