0
how they get not in dictionary
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")) results: >>> [2, 3, 4] None not in dictionary
3 Answers
+ 10
your keys are : 1, "orange" , True, None
as Tibor Santa explained, if the key is not in dictionary, the second parameter will be returned, or None if there's no second parameter given.
so you have three lines here.
1 : "orange" is a key in your dict, its value is returnes : [2,3,4]
2 : 7 is not a key, default "None" is returned
3 : 12345 is not a key, second parameter "not in dictionary" is returned.
To better understand, you may try
print(pairs.keys())
print(pairs.values())
+ 3
The second parameter of get() is the default value that is returned when the key does not exist in the dict.
0
Thanks