Python Tutorial Python Advanced Python References Python Libraries

Python Dictionary - copy() Method



The Python copy() method is used to copy the entire dictionary into a new dictionary. Any change done in new dictionary created by this method doesn't modify the old dictionary.

Syntax

dictionary.copy()

Parameters

No parameter is required.

Return Value

Returns a copy of the entire dictionary.

Example:

In the example below, the copy() method is used to create a copy of dictionary Info and save it into new variable called NewInfo.

Info = {
  'name': 'John',
  'age': 25,
  'city': 'London'
}
#copy 'Info' dictionary into 'NewInfo' dictionary.
NewInfo = Info.copy()   
print(NewInfo)

#modify new dictionary and print old dictionary
NewInfo['city'] = 'paris'
print(Info)

The output of the above code will be:

{'name': 'John', 'age': 25, 'city': 'London'}
{'name': 'John', 'age': 25, 'city': 'London'}

❮ Python Dictionary Methods

5