+ 2

Differentiation

I am trying to iterate through a list and remove the zeros in it, but Python keeps removing False also. I know False == 0 but.... Is there a way I can tell Python to leave False alone and deal only with 0 https://code.sololearn.com/cH0hAz4U4lA8/?ref=app

1st Jun 2020, 1:53 PM
Tomiwa Joseph
Tomiwa Joseph - avatar
6 ответов
1st Jun 2020, 2:24 PM
Avinesh
Avinesh - avatar
0
since False is singleton object,u can use kyword "is" instead to differentiate it..
1st Jun 2020, 2:03 PM
durian
durian - avatar
0
Lily Mea both 'is' and == removes False because False is 0 and False == 0
1st Jun 2020, 2:06 PM
Tomiwa Joseph
Tomiwa Joseph - avatar
0
Working code: https://code.sololearn.com/cksa4rBWs01e/?ref=app The same can be written with the usual "for" loop but annoying with the phone. Using method "remove" bad idea in many cases, because it removes the first element with the specified value, regardless of the program logic.
3rd Jun 2020, 6:32 PM
Leonid Selivanov
Leonid Selivanov - avatar
0
Amazing ideas guys. I eventually found out that the isinstance or type works very well with things like this.
3rd Jun 2020, 8:37 PM
Tomiwa Joseph
Tomiwa Joseph - avatar