dimanche 26 janvier 2020

How to remove Zero from the list but not False

I am trying to remove 0 from the list. I am using the Following if.

mylist = [False,1,0,1,2,0,1,3,"a"]

for i in mylist:
if i == 0 and not isinstance(i, bool):
    mylist.remove(i)
    mylist.insert(len(mylist),0)

print(mylist)

Problem is that this code treats the False as zero when. Things gets complicated when I change the if as follows:

for i in mylist:
if i == 0 and not isinstance(i, bool):
    print(i)

This will print only 0 not False. So it seems that the condition of the if works well until I do change the list.

Why this happens?

Aucun commentaire:

Enregistrer un commentaire