There are two ways by which we can copy one Dictionary to the other.
Let us look at the first way using the 'dict( )' method.
x = {
5 : "Is a Number",
"John": "Is a Name",
"Python": "Is a Language"
}
y = dict(x)
print("The Copied Dictionary is ",y)
So , in the above code we have created a 'Dictionary' and initialised to the variable 'x' .
x = {
5 : "Is a Number",
"John": "Is a Name",
"Python": "Is a Language"
}
Then we have used the 'dict( )' method that takes the Dictionary 'x' as parameter and creates a new Dictionary that would be the exact copy of 'x' .
Then assign it to 'y' .
And if we see the below output ,
The new variable 'y' is an exact copy of 'x' .
Now , let us look at the second way of copying a Dictionary using 'copy( )' method.
x = {
5 : "Is a Number",
"John": "Is a Name",
"Python": "Is a Language"
}
y = x.copy()
print("The Copied Dictionary is ",y)
Similarly , in the above code we have created a 'Dictionary' and initialised to the variable 'x' .
x = {
5 : "Is a Number",
"John": "Is a Name",
"Python": "Is a Language"
}
Then we have used the 'copy( )' method that Dictionary 'x' invokes and creates a new Dictionary that would be the exact copy of 'x' .
Then assign it to 'y' .
And if we see the below output ,
The new variable 'y' is an exact copy of 'x' .