[Solved] What does the get method do on dictionaries? [closed]


If the key argument is in switcher, the .get() method returns the value for the key.

If the key is not in the dictionary, the method returns the optional “nothing”.

def numbers_to_strings(argument):
    switcher = {0: "zero",
                1: "one",
                2: "two"}
    return switcher.get(argument, "nothing")

Calling the above function with a key that is in the dictionary:

>>> numbers_to_strings(0)
'zero'

And, calling the function with a key that is not in the dictionary:

>>> numbers_to_strings(3)
'nothing'

You could read about the method at dict.get()

Return the value for key if key is in the dictionary, else default. If default is not given, it defaults to None, so that this method never raises a KeyError.

solved What does the get method do on dictionaries? [closed]