Addem Addem - 7 months ago 11
Python Question

Python dictionary doesn't have all the keys assigned, or items

I created the following dictionary

exDict = {True: 0, False: 1, 1: 'a', 2: 'b'}


and when I print
exDict.keys()
, well, it gives me a generator. Ok, so I coerce it to a list, and it gives me

[False, True, 2]


Why isn't 1 there? When I print
exDict.items()
it gives me

[(False, 1), (True, 'a'), (2, 'b')]


Anyone have a guess about what's going on here? I'm stumped.

Answer

This happens because True == 1 (and False == 0, but you didn't have 0 as a key). You'll have to refactor your code or data somehow, because a dict considers keys to be the same if they are "equal" (rather than is).