Problem overview
The function safeLookup receives a dictionary scores that maps names to scores and a name. It returns the score of name, or 0 when name isn't a key. Unlike a plain lookup, this one must work for names that aren't there, and for an empty dictionary.
With {"Ana": 90, "Ben": 75, "Cara": 88}, the name "Ben" gives 75 and "Dan" gives 0. With {} and "Ana" the answer is 0.
Approach
The brackets scores[name] raise a KeyError when the key is missing, which would crash the function. One way around it is to test first with if name in scores. The dictionary method get does the test and the lookup in one call: scores.get(name, 0) returns the stored value if the key exists, and the second argument, 0, if it doesn't.
The longer form is worth seeing, because it spells out what get does:
def safe(scores, name):
if name in scores:
return scores[name]
return 0
It gives the same answers: 75 for {"Ben": 75} with "Ben", and 0 with "Dan". The get version is shorter and does the key search once instead of twice.
Step by step
- Return
scores.get(name, 0). The first argument is the key to look for and the second is the default for a missing key.
The solution in Python
class Solution:
def safeLookup(self, scores: Dict[str, int], name: str) -> int:
return scores.get(name, 0)
Details worth knowing:
- The default is optional.
scores.get("Dan")with no second argument returnsNonefor a missing key. Here the answer must be0, so the default is given explicitly. getdoesn't change the dictionary. It only reads. The missing key isn't added.- Keys match exactly. With the dictionary above,
"ana"isn't"Ana", soscores.get("ana", 0)gives0. - A stored 0 and a missing key look the same. With
{"Zed": 0}, looking up"Zed"returns0, because that is the stored score. Here that is fine, since both cases must answer0. When a missing key must be told apart from a stored value, useinor a default that can't be a real value, such asNone.
Edge cases and common mistakes
- Square brackets.
scores[name]works for"Ben"but raisesKeyErrorfor"Dan"and for any name looked up in an empty dictionary. - Forgetting the default.
scores.get(name)returnsNonefor a missing name, not0, and the tests expect the number. - Parentheses mix-up.
scores.get[name, 0]fails:getis a method, so it is called with parentheses.
Choosing the default
The default doesn't have to be a number. It is simply what get hands back when the key is missing, so scores.get(name, -1) would answer -1, and scores.get(name, "no score") would answer with that text. The lesson picks 0 because a name without a score counts as zero points, and the scores are all between 0 and 100. The same pattern is common in counting code, where a missing key means "seen zero times so far": counts.get(word, 0) + 1 is the new count.
For example, counting the words of a list this way, counts[w] = counts.get(w, 0) + 1, turns ["a", "b", "a"] into {'a': 2, 'b': 1}.
Testing
{"Ana": 90, "Ben": 75, "Cara": 88}with"Ben"gives75.- The same dictionary with
"Dan"gives0. - The same dictionary with
"ana"gives0. {}with"Ana"gives0.
