0
Dictionaries
Can anyone explain why the 3rd get. returns 'not in dictionary' ? Thanks...
10 ответов
+ 4
>>>
[2, 3, 4]
None
not in dictionary
>>>
+ 2
thanks..
+ 2
12345 isnt in your pairs dictionary. so it returns what you told it to return instead
+ 1
Ah okay thanks! So the "not in dictionary" 'overrides' the 12345 (which on its own would return "none"), as i understand it
+ 1
Bcoz in the 3rd get, the by default msg was 'not in dictionary'
0
provide your code
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"))
0
with the result being:
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)
0
if the 1st argument doesnt match any key, then it prints the 2nd aguments.