How to let Python know that not every 0 is False?

Please help! I don’t know how to make a for loop with variable != 0 where rest from a list (str, int, bool, float) is appending to new one just without 0 or 0.0. Every time I try False isn’t in new list and it should be!

@icodeso, I’m not sure what you mean. Is it something like this?

lst = [1,2,3]
print(False in lst)
print(False in lst)



In earlier versions of Python, there were no values True and False, just 1 and 0 (as with C and other languages.) True and False are a subtype of integers, and they can be used interchangeably in arithmetic expressions.

print(True + True)



(Additionally, in the case of if or while statements, or with and or or or not expressions, in addition to zero, any empty container, None or any expression that evaluates to one of these will be considered False, and any other object will be considered True.)

… so what is it that you are trying to do?


Thanks a lot, I should give an example in the first place. So please take a look:

def move_zeros(array):
  new = []
  for var in array:
    if var != 0:
  zeroes = len(array) - n
  for i in range(zeroes):
  array = new
  return array
#answer [9, 9, 1, 2, 1, 1, 3, 1, 9, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
#['a', 'b', 'c', 'd', 1, 1, 3, 1, 9, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
#['a', 'b', None, 'c', 'd', 1, 1, 3, [], 1, 9, {}, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] WHERE IS FALSE?!
#[1, None, 2, 1, 0, 0, 0]  WHERE IS FALSE?!

where should False be? At the end or just not removed in the first play?

you could use type() to check that 0 is an integer:

if not (var == 0 and type(var) == int):

This thread brings to mind a “trick” expression, courtesy of Dan Bader:

mystery_dict = {True: 'yes', 1: 'no', 1.0: 'maybe'}

What will it print???


@stetim94 I swear I tried! But it didn’t work. Maybe I had other mistake somewere else.
I spent so much time on it that I forgot about simple solutions. Thank you very much for helping me with this trivial problem. False should be not removed.
@patrickd314 this is interesting! Could you explain it somehow?

the comparison is quite tricky.

In addition to checking type(var) == bool or isinstance(var, bool), you could use is to check whether var is False.

(is is, in a sense, “stronger” than ==, but be careful, though, it can possibly be inconsistent if used with anything else except True, False, or None. Much more on this to be found via Dr. Google)

There’s a very nice explanation provided in the link in my earlier post.

1 Like

yeah, i didn’t notice. Thank you again, you deserve to be a Super User!