Python Dictionary copy() Method Tutorial

In this section, we will learn what the Dictionary copy() method is and how to use it in Python.

What is Dictionary copy() Method in Python?

The Python Dictionary copy() method is used to get a copy of the target dictionary.

Note that we’re saying a copy of the target dictionary! That means these two dictionaries are independent from each other and if one of them added or modified an item in its body, the other won’t take effect.

Python Dictionary copy() Method Syntax:

dictionary.copy()

Dictionary copy() Method Parameter:

The method does not take an argument.

Dictionary copy() Method Return Value

The return value of this method is a new dictionary that contains the copy of the elements in the target Dictionary.

Example: using python dictionary copy() method

dictionary = {

"name":"Jack",

"lastName":"Doe"

}

copyDictionary = dictionary.copy()

copyDictionary["age"] = 200

print(copyDictionary)

print(dictionary)

Output:

{'name': 'Jack', 'lastName': 'Doe', 'age': 200}

{'name': 'Jack', 'lastName': 'Doe'}
Facebook
Twitter
Pinterest
LinkedIn

Top Technologies