While Using Python Dictionary DataStructure (which contains key-value pair) if i want to retrieve some value from my Dictionary i have two options d[''] and g.get('key') so i am confused now which is better and Why ?? I understand both some way but when it comes to memory consumption and evaluation in memory which one is better ??
Hoping for some Positive reply,
Regards.
Python Dictionary get() Method The get() method returns the value of the item with the specified key.
The %d operator is used as a placeholder to specify integer values, decimals, or numbers. It allows us to print numbers within strings or other values. The %d operator is put where the integer is to be specified. Floating-point numbers are converted automatically to decimal values. Python3.
Syntax of the Python dictionary The syntax for a Python dictionary begins with the left curly brace ( { ), ends with the right curly brace ( } ), and contains zero or more key : value items separated by commas ( , ). The key is separated from the value by a colon ( : ).
You declare a dictionary with a set of curly braces, {} . Inside the curly braces you have a key-value pair. Keys are separated from their associated values with colon, : .
From the Python Library Docs
d[key]
Return the item of d with key key. Raises aKeyError
if key is not in the map.If a subclass of dict defines a method
__missing__()
, if the key key is not present, thed[key]
operation calls that method with the key key as argument. Thed[key]
operation then returns or raises whatever is returned or raised by the__missing__(key)
call if the key is not present. No other operations or methods invoke__missing__()
. If__missing__()
is not defined,KeyError
is raised.__missing__()
must be a method; it cannot be an instance variable. [...]
and
get(key[, default])
Return the value for key if key is in the dictionary, else default. If default is not given, it defaults toNone
, so that this method never raises aKeyError
.
The difference lies in the return value. When you ask for the value corresponding to a non-existing key, you either want
KeyError
raisedPython provides the different functionalities through multiple methods.
There will be a performance hit using []
when the key is not found, either in calling _missing_
or raising the exception. As to which one is faster when the key IS present, I checked the source code. (I used 2.7.2 for this check.) In dictobject.c
we see:
get
calls dict_get
[]
calls dict_subscript
Now if the values are present, in dict_get
we have
if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &failobj)) return NULL; if (!PyString_CheckExact(key) || (hash = ((PyStringObject *) key)->ob_shash) == -1) { hash = PyObject_Hash(key); if (hash == -1) return NULL; } ep = (mp->ma_lookup)(mp, key, hash);
and in dict_subscript
we have
assert(mp->ma_table != NULL); if (!PyString_CheckExact(key) || (hash = ((PyStringObject *) key)->ob_shash) == -1) { hash = PyObject_Hash(key); if (hash == -1) return NULL; ep = (mp->ma_lookup)(mp, key, hash);
The only difference is that get
does an extra unpack tuple!
Significant? I have no idea. :-)
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