+ 1
Why does the len of this dictionary say 3 and not 4? (Python)
From Sololearn Python Data Structures, page shows this example and also states you can use len() on dictionaries. pairs = {1: "apple", "orange": [2, 3, 4], True: False, 12: "True", } print(pairs.get("orange")) print(pairs.get(7, 42)) print(pairs.get(12345, "not found")) print(len(pairs)) Output: [2, 3, 4] 42 not found 3 Shouldn't it be 4? Edit: I think it has to do with the True:False entry, but I'm not sure why that is not being counted in the length.
5 Respuestas
+ 5
You're one the right track: True is the same key as 1. True: False overwritten 1: "apple"
Test it by printing out the whole dictionary
+ 2
LucaAntonieri
In some languages 1 means True and 0 means False.
So here True will override to 1 and False will override to 0
+ 2
Also test this:
print(True, int(True))
print(1, bool(1))
0
Thanks, I tested it, but why is ''True the same key as 1'', why does True:False overwrite it?
0
True overwrites 1 because it comes later