copy() method in Python Dictionary

In this article, we would cover how to use copy() method in Python dictionary. With copy() method we can have a copy of dictionary which merely points to the underlying values of the source dictionary. The copied dictionary would have same items as that of source dictionary.

The syntax for Dictionary copy() is –

dictionary_name.copy()

We cover more about it through relevant example next.

copy() method in Python Dictionary

Example. We would see how to merely copy the dictionary without making any changes. Let’s say we have a dictionary x and we want a copy of it as y. So,

x = {"name": "abc", "address":"XYZ"}
y = x.copy()
print("Dictionary x is: ", x)
print("Dictionary y is: ", y)

It would return with –

Dictionary x is: {'name': 'abc', 'address': 'XYZ'}
Dictionary y is: {'name': 'abc', 'address': 'XYZ'}

Clearly, Dictionary y is a copy of Dictionary x.

In conclusion, we have covered copy() method in Python dictionary here.