[Python] Why does removing items from list while iterating over it skip every other item...

Discussão em 'Python' iniciado por Stack, Agosto 17, 2025.

  1. Stack

    Stack Membro Participativo

    When iterating over a list and removing each item, why is every other item skipped and not removed? (I realise it may be bad practice to do this I just want to understand what is going on)

    lst=[0,1,2,3,4,5,6,7,8,9]
    for item in lst:
    lst.remove(item)
    print(lst)


    Expected output: [] Actual output: [1, 3, 5, 7, 9]

    Continue reading...

Compartilhe esta Página