Why does the len of this dictionary say 3 and not 4? (Python) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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.

5th Dec 2021, 9:41 PM
LucaAntonieri
5 Answers
+ 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
5th Dec 2021, 10:32 PM
Lisa
Lisa - avatar
+ 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
6th Dec 2021, 10:54 AM
A͢J
A͢J - avatar
+ 2
Also test this: print(True, int(True)) print(1, bool(1))
6th Dec 2021, 11:01 AM
Lisa
Lisa - avatar
0
Thanks, I tested it, but why is ''True the same key as 1'', why does True:False overwrite it?
6th Dec 2021, 10:51 AM
LucaAntonieri
0
True overwrites 1 because it comes later
8th Dec 2021, 7:43 AM
Ivan Petriv
Ivan Petriv - avatar