0

Dictionaries

Can anyone explain why the 3rd get. returns 'not in dictionary' ? Thanks...

24th Jan 2018, 8:26 PM
Joseph
10 Respuestas
+ 4
>>> [2, 3, 4] None not in dictionary >>>
24th Jan 2018, 8:29 PM
Joseph
+ 2
thanks..
24th Jan 2018, 8:30 PM
Joseph
+ 2
12345 isnt in your pairs dictionary. so it returns what you told it to return instead
24th Jan 2018, 8:30 PM
Jeremy
Jeremy - avatar
+ 1
Ah okay thanks! So the "not in dictionary" 'overrides' the 12345 (which on its own would return "none"), as i understand it
24th Jan 2018, 8:35 PM
Joseph
+ 1
Bcoz in the 3rd get, the by default msg was 'not in dictionary'
1st Feb 2021, 10:13 AM
Arijit Hazra
Arijit Hazra - avatar
0
provide your code
24th Jan 2018, 8:27 PM
Jeremy
Jeremy - avatar
0
pairs = {1: "apple", "orange": [2, 3, 4], True: False, None: "True", } print(pairs.get("orange")) print(pairs.get(7)) print(pairs.get(12345, "not in dictionary"))
24th Jan 2018, 8:28 PM
Joseph
0
with the result being:
24th Jan 2018, 8:29 PM
Joseph
0
the second argument for the get method is an optional value that is used if your dictionary doesnt contain whatever you are trying to get with the first argument. in short: get(whatYouActuallyWant, whatToGetIfItFails)
24th Jan 2018, 8:43 PM
Jeremy
Jeremy - avatar
0
if the 1st argument doesnt match any key, then it prints the 2nd aguments.
20th Jun 2020, 5:15 AM
Pranjal Pravesh
Pranjal Pravesh - avatar