Today, I came across the dict
method get
which, given a key in the dictionary, returns the associated value.
For what purpose is this function useful? If I wanted to find a value associated with a key in a dictionary, I can just do dict[key]
, and it returns the same thing:
dictionary = {"Name": "Harry", "Age": 17} dictionary["Name"] dictionary.get("Name")
get() is usually preferred, as it accepts a second argument which acts as the default value shall the key not exist in the given dictionary. Due to this property, dict. get() will always return a value, whereas dict[key] will raise a KeyError if the given key is missing.
With CPython 2.7, using dict() to create dictionaries takes up to 6 times longer and involves more memory allocation operations than the literal syntax. Use {} to create dictionaries, especially if you are pre-populating them, unless the literal syntax does not work for your case.
You can use the get() method of the dictionary ( dict ) to get any default value without an error if the key does not exist. Specify the key as the first argument. The corresponding value is returned if the key exists, and None is returned if the key does not exist.
The difference between the dict[key] syntax and dict. get() is that if a key is not found using the dict[key] syntax, a KeyError is raised. If you use dict. get() and a key is not found, the code will return None (or a custom value, if you specify one).
It allows you to provide a default value if the key is missing:
dictionary.get("bogus", default_value)
returns default_value
(whatever you choose it to be), whereas
dictionary["bogus"]
would raise a KeyError
.
If omitted, default_value
is None
, such that
dictionary.get("bogus") # <-- No default specified -- defaults to None
returns None
just like
dictionary.get("bogus", None)
would.
What is the
dict.get()
method?
As already mentioned the get
method contains an additional parameter which indicates the missing value. From the documentation
get(key[, default])
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
.
An example can be
>>> d = {1:2,2:3} >>> d[1] 2 >>> d.get(1) 2 >>> d.get(3) >>> repr(d.get(3)) 'None' >>> d.get(3,1) 1
Are there speed improvements anywhere?
As mentioned here,
It seems that all three approaches now exhibit similar performance (within about 10% of each other), more or less independent of the properties of the list of words.
Earlier get
was considerably slower, However now the speed is almost comparable along with the additional advantage of returning the default value. But to clear all our queries, we can test on a fairly large list (Note that the test includes looking up all the valid keys only)
def getway(d): for i in range(100): s = d.get(i) def lookup(d): for i in range(100): s = d[i]
Now timing these two functions using timeit
>>> import timeit >>> print(timeit.timeit("getway({i:i for i in range(100)})","from __main__ import getway")) 20.2124660015 >>> print(timeit.timeit("lookup({i:i for i in range(100)})","from __main__ import lookup")) 16.16223979
As we can see the lookup is faster than the get as there is no function lookup. This can be seen through dis
>>> def lookup(d,val): ... return d[val] ... >>> def getway(d,val): ... return d.get(val) ... >>> dis.dis(getway) 2 0 LOAD_FAST 0 (d) 3 LOAD_ATTR 0 (get) 6 LOAD_FAST 1 (val) 9 CALL_FUNCTION 1 12 RETURN_VALUE >>> dis.dis(lookup) 2 0 LOAD_FAST 0 (d) 3 LOAD_FAST 1 (val) 6 BINARY_SUBSCR 7 RETURN_VALUE
Where will it be useful?
It will be useful whenever you want to provide a default value whenever you are looking up a dictionary. This reduces
if key in dic: val = dic[key] else: val = def_val
To a single line, val = dic.get(key,def_val)
Where will it be NOT useful?
Whenever you want to return a KeyError
stating that the particular key is not available. Returning a default value also carries the risk that a particular default value may be a key too!
Is it possible to have
get
like feature indict['key']
?
Yes! We need to implement the __missing__
in a dict subclass.
A sample program can be
class MyDict(dict): def __missing__(self, key): return None
A small demonstration can be
>>> my_d = MyDict({1:2,2:3}) >>> my_d[1] 2 >>> my_d[3] >>> repr(my_d[3]) 'None'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With