[Python] removing multiples from a list of numbers

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

  1. Stack

    Stack Membro Participativo

    I am trying to write code that removes multiples of 5 from the list.

    multiples = [5, 10, 15, 20, 21, 31, 41]

    for number in multiples:
    if number % 5 == 0:
    multiples.remove(number)
    print(multiples)


    This is the output I get:

    [10, 20, 21, 31, 41]


    Why does this code produce output that includes numbers divisible by 5?

    Continue reading...

Compartilhe esta Página