Manipulating dict¶
Let us understand how we can manipulate the dicts in Python.* We can add new key value pairs to dict
by using typical assignment.
- We can also use assignment operation to update existing key value pair in the
dict
. setdefault
can be used to get the element from thedict
by using key. If key does not exist, it will update thedict
with the key passed along with default value.update
can be used to merge a list of pairs (2 tuples) or adict
into thedict
.- Elements from the dict can be removed using functions like
pop
andpopitem
.pop
is typically used to remove the element using key.popitem
is used to remove one of the item (typically last) from thedict
.
In [1]:
d = {'id': 1, 'first_name': 'Scott', 'last_name': 'Tiger', 'amount': 1000.0}
In [2]:
d['commission_pct'] = 10 # Adding Element
In [3]:
d['phone_numbers'] = 1234567890
In [4]:
d
Out[4]:
{'id': 1, 'first_name': 'Scott', 'last_name': 'Tiger', 'amount': 1000.0, 'commission_pct': 10, 'phone_numbers': 1234567890}
In [5]:
d['amount'] = 1500.0
In [6]:
d
Out[6]:
{'id': 1, 'first_name': 'Scott', 'last_name': 'Tiger', 'amount': 1500.0, 'commission_pct': 10, 'phone_numbers': 1234567890}
In [7]:
d = {'id': 1, 'first_name': 'Scott', 'last_name': 'Tiger', 'amount': 1000.0}
In [8]:
d.setdefault?
Signature: d.setdefault(key, default=None, /) Docstring: Insert key with a value of default if key is not in the dictionary. Return the value for key if key is in the dictionary, else default. Type: builtin_function_or_method
In [9]:
d.setdefault('amount')
Out[9]:
1000.0
In [10]:
d.setdefault('commission_pct')
In [11]:
d
Out[11]:
{'id': 1, 'first_name': 'Scott', 'last_name': 'Tiger', 'amount': 1000.0, 'commission_pct': None}
In [12]:
d = {'id': 1, 'first_name': 'Scott', 'last_name': 'Tiger', 'amount': 1000.0}
In [13]:
d
Out[13]:
{'id': 1, 'first_name': 'Scott', 'last_name': 'Tiger', 'amount': 1000.0}
In [14]:
d.setdefault('commission_pct', 0)
Out[14]:
0
In [15]:
d
Out[15]:
{'id': 1, 'first_name': 'Scott', 'last_name': 'Tiger', 'amount': 1000.0, 'commission_pct': 0}
In [16]:
d.setdefault('commission_pct', 100)
Out[16]:
0
In [17]:
d
Out[17]:
{'id': 1, 'first_name': 'Scott', 'last_name': 'Tiger', 'amount': 1000.0, 'commission_pct': 0}
In [18]:
d.update?
Docstring: D.update([E, ]**F) -> None. Update D from dict/iterable E and F. If E is present and has a .keys() method, then does: for k in E: D[k] = E[k] If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v In either case, this is followed by: for k in F: D[k] = F[k] Type: builtin_function_or_method
In [19]:
d = {'id': 1}
In [20]:
d
Out[20]:
{'id': 1}
In [21]:
d.update({'first_name': 'Donald', 'last_name': 'Duck'})
In [22]:
d
Out[22]:
{'id': 1, 'first_name': 'Donald', 'last_name': 'Duck'}
In [23]:
d.update([('amount', 1000.0), ('commission_pct', 10)])
In [24]:
d
Out[24]:
{'id': 1, 'first_name': 'Donald', 'last_name': 'Duck', 'amount': 1000.0, 'commission_pct': 10}
In [25]:
d.update([('amount', 1500.0), ('commission_pct', 5), ('phone_numbers', 1234567890)])
In [26]:
d
Out[26]:
{'id': 1, 'first_name': 'Donald', 'last_name': 'Duck', 'amount': 1500.0, 'commission_pct': 5, 'phone_numbers': 1234567890}
In [27]:
d = {'id': 1, 'first_name': 'Scott', 'last_name': 'Tiger', 'amount': 1000.0}
In [28]:
d['commission_pct'] = 10 # Adding Element
In [29]:
d['phone_numbers'] = 1234567890
In [30]:
d
Out[30]:
{'id': 1, 'first_name': 'Scott', 'last_name': 'Tiger', 'amount': 1000.0, 'commission_pct': 10, 'phone_numbers': 1234567890}
In [31]:
d.pop('phone_numbers')
Out[31]:
1234567890
In [32]:
d
Out[32]:
{'id': 1, 'first_name': 'Scott', 'last_name': 'Tiger', 'amount': 1000.0, 'commission_pct': 10}
In [33]:
d.pop('phone_numbers') # throws KeyError
--------------------------------------------------------------------------- KeyError Traceback (most recent call last) Input In [33], in <cell line: 1>() ----> 1 d.pop('phone_numbers') KeyError: 'phone_numbers'
In [34]:
d.pop('phone_numbers', 'No such key exists')
Out[34]:
'No such key exists'
In [35]:
d.pop?
Docstring: D.pop(k[,d]) -> v, remove specified key and return the corresponding value. If key is not found, d is returned if given, otherwise KeyError is raised Type: builtin_function_or_method
In [36]:
d
Out[36]:
{'id': 1, 'first_name': 'Scott', 'last_name': 'Tiger', 'amount': 1000.0, 'commission_pct': 10}
In [37]:
d.popitem?
Signature: d.popitem() Docstring: Remove and return a (key, value) pair as a 2-tuple. Pairs are returned in LIFO (last-in, first-out) order. Raises KeyError if the dict is empty. Type: builtin_function_or_method
In [38]:
d.popitem()
Out[38]:
('commission_pct', 10)
In [39]:
d
Out[39]:
{'id': 1, 'first_name': 'Scott', 'last_name': 'Tiger', 'amount': 1000.0}