Accessing Elements – dict¶
Let us see how we can access elements from the dict
using Python as programming language.* We can access a value of a particular element in dict
by passing key d[key]
. If the key does not exists, it will throw KeyError.
get
also can be used to access a value of particular element indict
by passing key as argument. However, if key does not exists, it will return None.- We can also pass a default value to
get
. - We can get all the keys in the form of set like object by using
keys
and all the values in the form of list like object by usingvalues
. - We can also use
items
to convert adict
into a set like object with pairs. Each element (which is a pair) in the set like object will be a tuple. - Let us see few examples.
- Creating a dict object
In [1]:
d = {'id': 1, 'first_name': 'Scott', 'last_name': 'Tiger', 'amount': 1000.0}
- Accessing one of the element with key id from
dict
.
In [2]:
d['id']
Out[2]:
1
- Accessing another element with key first_name from
dict
.
In [3]:
d['first_name']
Out[3]:
'Scott'
In [4]:
d['commission_pct'] # throws key error
--------------------------------------------------------------------------- KeyError Traceback (most recent call last) Input In [4], in <cell line: 1>() ----> 1 d['commission_pct'] KeyError: 'commission_pct'
- We can also use
get
to get the value by passing the key as argument to it.
In [5]:
d.get?
Signature: d.get(key, default=None, /) Docstring: Return the value for key if key is in the dictionary, else default. Type: builtin_function_or_method
In [6]:
d.get('first_name')
Out[6]:
'Scott'
- If the key does not exists it will return
None
(unliked['commission_pct']
which threw key error.
In [7]:
d.get('commission_pct') # Returns None
- You can pass default value as second argument to
get
. If the key exists it will return the value fromdict
, if not it will return the default value which is passed as second argument.
In [8]:
d.get('first_name', 'Some First Name')
Out[8]:
'Scott'
In [9]:
d.get('commission_pct', 0)
Out[9]:
0
In [10]:
d.keys?
Docstring: D.keys() -> a set-like object providing a view on D's keys Type: builtin_function_or_method
In [11]:
d.keys() # returns all the keys in set like object (as keys are unique)
Out[11]:
dict_keys(['id', 'first_name', 'last_name', 'amount'])
In [12]:
d.values?
Docstring: D.values() -> an object providing a view on D's values Type: builtin_function_or_method
In [13]:
d.values() # returns all the values in list like object.
Out[13]:
dict_values([1, 'Scott', 'Tiger', 1000.0])
In [14]:
d.items?
Docstring: D.items() -> a set-like object providing a view on D's items Type: builtin_function_or_method
In [15]:
d.items()
# returns all the items in set like object.
# Each item will be of type tuple
# Each tuple will contain key from the dict followed by corresponding value
Out[15]:
dict_items([('id', 1), ('first_name', 'Scott'), ('last_name', 'Tiger'), ('amount', 1000.0)])
- We can convert keys or values or items to list and perform any list operations.
In [16]:
list(d.items())[0] # first tuple from the list of items from dict
Out[16]:
('id', 1)
In [17]:
list(d.items())[1] # second tuple from the list of items from dict
Out[17]:
('first_name', 'Scott')
In [18]:
type(list(d.items())[1])
Out[18]:
tuple