[Python] Why does removing elements from a list during a loop in Python skip items? [duplicate]

Discussão em 'Python' iniciado por Stack, Julho 12, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to remove items from a Python list while iterating over it. Here's my code:

    nums = [10, 15, 20, 25, 30]
    for num in nums:
    if num % 10 == 0:
    nums.remove(num)
    print(nums)

    Continue reading...

Compartilhe esta Página