Python Dictionary copy()

The python dictionary copy() method is used to get a copy of the dictionary. You can copy the Python dictionary in different ways. copy() method does the shallow copy of the dictionary. The dictionary can also be copied using the = operator, which points to the same object as the original. So if any changes are made to the copied dictionary will also reflect in the original dictionary.

In this article, I will explain how to copy the python dictionary to another by using the copy() method and equal operator (=) with examples

All dictionary methods are available on the dictionary methods page..

Quick examples of Python dictionary copy()

Following are the quick examples of the Python Dictionary copy() method.


technology = { 'course':'python', 'fee': 4000, 'duration': '60 days' }
#  Example 1: Using copy() method copy the My_cubes dictionary
new_technology = technology.copy()
print("Original technology: ",technology)
print("New technology: ",new_technology)

# Example 2: Using del keyword delete the fee: 4000 element in New dictionary
new_technology=technology.copy()
del new_technology['fee']
print("Original technology: ",technology)
print("New technology: ",new_technology)

# Example 3: Copy the dictionary element by element 
new_technology=dict()
for i in technology:
    new_technology[i]=technology[i]
print("Original technology: ",technology)
print("New technology: ",new_technology) 

# Example 4: Using = operator copy the technology dictionary
technology = { 'course':'python', 'fee': 4000, 'duration': '60 days' }
new_technology=technology
print("Original technology: ",technology)
print("New technology: ",new_technology)   

# Example 5: Using clear() method delete the New_cubes dictionary
technology = { 'course':'python', 'fee': 4000, 'duration': '60 days' }
new_technology=technology
new_technology. clear()
print("Original technology: ",technology)
print("New technology: ",new_technology) 
  

1. Syntax of the copy() method

Syntax of the Python Dictionary copy() method.


# Syntax of copy()
dict.copy()

1.1 copy() parameters

The copy() method doesn’t allow parameters.

1.2 copy() return value

The copy() method returns a shallow copy of the dictionary. It doesn’t allow any modification of the original dictionary.

2. Usage of Python Dictionary copy() method

copy() method does the shallow copy of the dictionary meaning any changes to the original or copied dictionary will not be reflected on the other dictionary. Basically it does copy by values. Though Python has different other ways to copy the dict, this is the best way to use.

2.1 Dictionary copy() Examples

Using the dictionary copy() method we can copy every element of the dictionary. In the below example technology is the original dict and new_technology is created from the copy() method.


# Using copy() method copy the My_cubes dictionary
technology = { 'course':'python', 'fee': 4000, 'duration': '60 days' }
new_technology=technology.copy()
print("Original technology: ",technology)
print("New technology: ",new_technology)

# Output:
Original technology:  {'course': 'python', 'fee': 4000, 'duration': '60 days'}
New technology:  {'course': 'python', 'fee': 40000, 'duration': '60 days'}

2.2 Copy Dictionary And Update

When a dictionary is copied using the copy() method, any changes in a new dictionary, will not be reflected or applied in the original dictionary. Let’s take an example.


# Using del keyword delete the fee: 4000 element in New dictionary
technology = { 'course':'python', 'fee': 4000, 'duration': '60 days' }
new_technology=technology.copy()
del new_technology['fee']
print("Original technology: ",technology)
print("New technology: ",new_technology)

# Outputs:
Original technology:  {'course': 'python', 'fee': 4000, 'duration': '60 days'}
New technology:  {'course': 'python', 'duration': '60 days'}

Notice that New dictionary is changed, the Original dictionary remains unchanged.

3. Copy the Dictionary Element by Element

We can also iterate over the entire dictionary and copy every element by element pointed by the key to a new dictionary that has previously created. For Example,


# copy the dictionary element by element 
technology = { 'course':'python', 'fee': 4000, 'duration': '60 days' }
new_technology=dict()
for i in technology:
    new_technology[i]=technology[i]
print("Original technology: ",technology)
print("New technology: ",new_technology)   

# Outputs:
Original technology:  {'course': 'python', 'fee': 4000, 'duration': '60 days'}
New technology:  {'course': 'python', 'fee': 4000, 'duration': '60 days'}

# Modifying the copied dictionary
# and checking the change of my_cubes
new_technology['tutor']='Richard'
print("Original technology: ",technology)
print("New technology: ",new_technology)  
 
# Output:
Original technology:  {'course': 'python', 'fee': 4000, 'duration': '60 days'}
New technology:  {'course': 'python', 'fee': 4000, 'duration': '60 days', 'tutor': 'Richard'}

Note that in the last example updates on copied dictionary new_technology changes haven’t reflected in the original dictionary named technology.

4. Usage of = operator

Using the = operator we can also copy the dictionary, which allows the same object as the original. So if any changes made in a new dictionary will also reflected in the original dictionary.


# Using =  operator copy the technology dictionary
technology = { 'course':'python', 'fee': 4000, 'duration': '60 days' }
new_technology=technology
print("Original technology: ",technology)
print("New technology: ",new_technology)   
# Outputs:
Original technology:  {'course': 'python', 'fee': 4000, 'duration': '60 days'}
New technology:  {'course': 'python', 'fee': 4000, 'duration': '60 days'}

Let’s update and check the results.


# Using clear() method delete the New_cubes dictionary
technology = { 'course':'python', 'fee': 4000, 'duration': '60 days' }
new_technology=technology
new_technology. clear()
print("Original technology: ",technology)
print("New technology: ",new_technology)   

# Outputs:
Original technology:  {}
New technology:  {}

Conclusion

In this article, I have explained about Python dictionary copy() method with examples. Also, I have explained using = operator how to copy the dictionaries and what happens when you update the copied dictionary.

Happy Learning !!

References

Related Articles

Leave a Reply

You are currently viewing Python Dictionary copy()