Dictionary methods
In this section, we will discuss the dictionary methods one by one. Consider that you want to create a copy of an existing dictionary; you can use the copy()
method.
copy()
The syntax of the copy()
method is as follows:
dict.copy()
See the following example:
>>> Avengers ={'iron-man':"Tony", "CA":"Steve","BW":"Natasha"} >>> Avengers {'iron-man': 'Tony', 'CA': 'Steve', 'BW': 'Natasha'} >>> Avengers2 = Avengers.copy() >>> Avengers2 {'iron-man': 'Tony', 'CA': 'Steve', 'BW': 'Natasha'} >>>
You can see that Avengers2
is an exact copy of Avengers
. Do not confuse copy()
with the assignment operator. Let's see the following example:
>>> A1 = {'iron-man':"Tony", "CA":"Steve","BW":"Natasha"} >>> A2= A1 >>> >>> A2 {'iron-man': 'Tony', 'CA': 'Steve', 'BW': 'Natasha'} >>> >>> CW= A1.copy() >>> CW {'iron-man': 'Tony', 'CA': 'Steve', 'BW': 'Natasha'} >>>
Variable A1
and A2
...