Addem Addem - 2 years ago 59
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
, 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
it gives me

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

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

Answer Source

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).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download