Dictionary Methods
Python provides dictionary methods that allow us to work with dictionaries.
- copy()
- get()
- update()
- fromkeys()and more..
Let’s learn few among them
Referring Same Dictionary Object
Code
PYTHON
Output
Copy of Dictionary
dict.copy()
returns copy of a dictionary.Code
PYTHON
Output
Copy of List
Code
PYTHON
Output
Operations on Dictionaries
- len()
- clear()
Membership Check
Code
PYTHON
Output
Iterating
Cannot add/remove dictionary keys while iterating the dictionary.
Code
PYTHON
Output
Arbitrary Function Arguments
Passing Multiple Values
We can define a function to receive any number of arguments.
We have already seen such functions
- max(*args) max(1,2,3..)
- min(*args) min(1,2,3..)
Variable Length Arguments
Variable length arguments are packed as tuple.
Code
PYTHON
Output
Unpacking as Arguments
If we already have the data required to pass to a function as a sequence, we can
unpack it with
*
while passing.Code
PYTHON
Output
Multiple Keyword Arguments
We can define a function to receive any number of keyword arguments.
Variable length kwargs are packed as dictionary.
Code
PYTHON
Output
Iterating
kwargs is a dictionary. We can iterate over them like any other dictionary.
Code
PYTHON
Output
Unpacking as Arguments
Code - 1
PYTHON
Output
Code - 2
PYTHON