Empty list is equal to None or not?

user1559873 picture user1559873 · Dec 10, 2012 · Viewed 35.1k times · Source

Possible Duplicate:
Why does “[] == False” evaluate to False when “if not []” succeeds?

I am new to python as per ternary operator of python

>>> 'true' if True else 'false'  true
   true

i am expecting for below code output as [] because [] not equal to None

>>> a=[]
>>> a==None
False
>>> a if a else None
None

pleas correct if i am wrong

Thanks hema

Answer

jdotjdot picture jdotjdot · Dec 10, 2012

The empty list, [], is not equal to None.

However, it can evaluate to False--that is to say, its "truthiness" value is False. (See the sources in the comments left on the OP.)

Because of this,

>>> [] == False
False
>>> if []:
...     print "true!"
... else:
...     print "false!"
false!