[Python] Why is the output for python code with remove() wrong? [duplicate]

Discussão em 'Python' iniciado por Stack, Setembro 1, 2025.

  1. Stack

    Stack Membro Participativo

    When running this code :

    list1 = [1,2,4,6,4,5,3,2,1,8,12]
    for i in list1:
    if i%2==0:
    list1.remove(i)
    print(list1)


    output is : [1, 4, 4, 5, 3, 1, 12]

    why is 4 and 12 still there in the output? Both 4 and 12 are divisible by 2.

    Continue reading...

Compartilhe esta Página