False and 0 in the same dictionary

Prateek surekap at gmail.com
Tue Nov 4 15:48:53 EST 2008


I've been using Python for a while (4 years) so I feel like a moron
writing this post because I think I should know the answer to this
question:

How do I make a dictionary which has distinct key-value pairs for 0,
False, 1 and True.
As I have learnt, 0 and False both hash to the same value (same for 1
and True).

>>> b = {0:'xyz', False:'abc'}
>>> b
{0: 'abc'}  # Am I the only one who thinks this is weird?

This obviously stems from the fact that 0 == False but 0 is not False
etc. etc.

That doesn't help my case where I need to distinguish between the two

The same issue applies in a list:

Suppose I do:

>>> a = [0, 1, True, False]
>>> a.index(False)
0

Wha??? Help.





More information about the Python-list mailing list