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)
lst.append(0)
print(False in lst)

Output:

False
True

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)

Output:

2

(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?

4 Likes

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

def move_zeros(array):
  n=0
  new = []
  for var in array:
    if var != 0:
      new.append(var)
      n+=1
  zeroes = len(array) - n
  for i in range(zeroes):
    new.append(0)
  array = new
  return array
 
 
print(move_zeros([9,0.0,0,9,1,2,0,1,0,1,0.0,3,0,1,9,0,0,0,0,9])) 
#answer [9, 9, 1, 2, 1, 1, 3, 1, 9, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
print(move_zeros(["a",0,0,"b","c","d",0,1,0,1,0,3,0,1,9,0,0,0,0,9])) 
#['a', 'b', 'c', 'd', 1, 1, 3, 1, 9, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
print(move_zeros(["a",0,0,"b",None,"c","d",0,1,False,0,1,0,3,[],0,1,9,0,0,{},0,0,9]))
#['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?!
print(move_zeros([0,1,None,2,False,1,0]))
#[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):
3 Likes

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

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

What will it print???

3 Likes

@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!

2 Likes