Dictionaries | SoloLearn: Learn to code for FREE!

+1

Dictionaries

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

1/24/2018 8:26:01 PM

Joseph

9 Answers

New Answer

+3

>>> [2, 3, 4] None not in dictionary >>>

+1

with the result being:

+1

thanks..

+1

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

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

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.