jeudi 12 mars 2020

How do I del an element from list when using for loop and if statement [duplicate]

I am trying to delete all the even numbers from my list. I can see that the for loop and if statement is correctly identifying the even numbers but the del statement is not deleting that number from the list. Where is my logical error?

  list = [1,2,3,4,5,6]

    for num in list:
        if num % 2 == 0:
            print(num)
            del num

    print(list)

Aucun commentaire:

Enregistrer un commentaire