dictionary get in python | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

dictionary get in python

Is this a case where python cannot differentiate between True value and 1 in a dictionary?..Its the reason I added "true" ...do you have an explanation? or am i just wrong and missing something? pairs = {1: "apple", "orange": [2, 3, 4], "true": False, 12: "True", } print(pairs.get(1, "not found")) pairs = {1: "apple", "orange": [2, 3, 4], True: False, 12: "True", } print(pairs.get(1, "not found")) >>>apple >>> False

28th Dec 2021, 1:06 PM
Jamari McFarlane
Jamari McFarlane - avatar
10 Answers
+ 8
Jamari McFarlane , maybe this information can help you: python documentation: (bool)These represent the truth values False and True. The two objects representing the values False and True are the only Boolean objects. The Boolean type is a subtype of the integer type, and Boolean values behave like the values 0 and 1, respectively, in almost all contexts, the exception being that when converted to a string, the strings "False" or "True" are returned, respectively. this can be checked with following code: print(bool.__mro__) # mro is "Method Resolution Order" that shows an object hierarchy. the result of the above-mentioned code is: (<class 'bool'>, <class 'int'>, <class 'object'>) # this proves the facts mentioned above. knowing this we can do arithmetical operations with boolean values: print(8 + True + True) # result is 10
28th Dec 2021, 6:06 PM
Lothar
Lothar - avatar
+ 3
Output was "apple" and False, not True and False ... (corrected) Can you tell me what is your expectation of output? cause I'm not clearly getting what your problem is ...
28th Dec 2021, 1:37 PM
Ipang
+ 2
Your prediction was correct as you can see. The item value having key 1 is overwritten by value of the next item with "truthy" expression as key, in this case `True : False` What is your goal though?
28th Dec 2021, 2:15 PM
Ipang
+ 2
Print the dictionaries after they are created, you'll see why it goes like that by seeing the actual items that are created ...
28th Dec 2021, 2:20 PM
Ipang
+ 1
Ipang yea that was mistake but the question still remains
28th Dec 2021, 1:47 PM
Jamari McFarlane
Jamari McFarlane - avatar
+ 1
I see, but again, you still not telling me what output was expected. Or the essence of the doubt ...
28th Dec 2021, 2:02 PM
Ipang
+ 1
Ipang in the first example when "true" is mapped to False...when we ask for the value of the key 1 we get what we expect "apple"...but when I replace "true" with True...the value of 1 is False..
28th Dec 2021, 2:06 PM
Jamari McFarlane
Jamari McFarlane - avatar
+ 1
the only difference between the code is "true" and True.. which i used to test if True was messing with the 1...since 1 stands for True(well any number except 0 does)
28th Dec 2021, 2:08 PM
Jamari McFarlane
Jamari McFarlane - avatar
+ 1
@Ipang nothing..i was just reviewing python and thought that was behaving weird
28th Dec 2021, 2:18 PM
Jamari McFarlane
Jamari McFarlane - avatar
0
The second output is False because in python 1 = True and 0 = False and when you try to get 1 you actually get False because 1 is True and True is assigned to False in the dictionary. Hope it helps 😁
29th Dec 2021, 8:19 PM
Yair