-1

I was working on a project and stumbled across this weird anomaly, apparently the Boolean value for any list or tuple with an None value is True

Input

print(bool([])) # empty list
print(bool(())) # empty tuple

print(bool([None])) # list with None  
print(bool((None,))) # tuple with None

Output

False
False

True
True

can someone give a brief explanation as to why an list/tuple object with presumably None(null) value will have a Boolean value of True instead of False?

Amaan
  • 5
  • 5
  • 4
    " apparently the Boolean value for any list or tuple with an None value is True" Of course, **any non-empty list or tuple** (or any built-in collection) is *truthy*. This isn't an "weird anomaly". This is clearly documented and unambiguous – juanpa.arrivillaga Nov 07 '22 at 19:18
  • 2
    As to "why", well, the answer is *because that is what the language designers decided should be the behavior*. – juanpa.arrivillaga Nov 07 '22 at 19:18

1 Answers1

3

A tuple or a list evaluate to False in a boolean context if they're empty. (None,) and [None] are a tuple/list with 1 element (the element None), hence not empty... therefore they evaluate to True in a boolean context

Alberto Garcia
  • 324
  • 1
  • 11