Python dict.get('key') versus dict['key'] [duplicate] Python dict.get('key') versus dict['key'] [duplicate] python python

Python dict.get('key') versus dict['key'] [duplicate]


This is simply how the get() method is defined.

From the Python docs:

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.

The default "not-found" return value is None. You can return any other default value.

d = dict()d.get('xyz', 42)  # returns 42


Accessing by brackets does not have a default but the get method does and the default is None. From the docs for get (via a = dict(); help(a.get))

Help on built-in function get:get(...)    D.get(k[,d]) -> D[k] if k in D, else d.  d defaults to None.


Simply because [ 1 ] the key is not in the map and [ 2 ] those two operations are different in nature.

From dict Mapping Types:

d[key]

Return the item of d with key key. Raises a KeyError if key is not in the map.

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.