Python Copy a Dictionary with Best Example

In this Python copy a dictionary tutorial, we will learn how to copy a dictionary with the best examples and illustrations.

Copy() Syntax

dict.copy()

Copy() Arguments

There are no arguments for the copy() method.

Copy() Return Value

This method makes a shallow copy of the dictionary and returns it. The original dictionary is not changed.

Can you copy a dictionary in Python?

It is possible to copy a dictionary in Python. In this tutorial, we are going to use different methods to show how you can copy a dictionary.

We can copy a dictionary using a copy() function. For example,

original_dict = {1:"a", 2:"b", 3:"c"}
new_dict = original_dict.copy()

print("The new copy:",new_dict)
print("The original:",original_dict)

Output:

The new copy: {1: 'a', 2: 'b', 3: 'c'}
The original: {1: 'a', 2: 'b', 3: 'c'}

How do you copy in Python?

In Python, a copy of an object is created with the = operator. You could think that this produces a new object, but it does not. It just creates a new variable that shares the old object’s reference.

Example of copy dictionary Python

The following are examples of how to copy dictionaries in Python.

Using the copy() to copy Dictionaries

original_dict = {1:"a", 2:"b", 3:"c", 4:"d", 5:"e"}
new_dict = original_dict.copy()

# clear the new copy object
new_dict.clear()

print("The new copy:",new_dict)
print("The original:",original_dict)

Output:

The new copy: {}
The original: {1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'e'}

Using the = operator to copy Dictionaries

original_dict = {1:"a", 2:"b", 3:"c", 4:"d", 5:"e"}
new_dict = original_dict

# clear the new copy object
new_dict.clear()

print("The new copy:",new_dict)
print("The original:",original_dict)

Output:

The new copy: {}
The original: {}

The difference between shallow copy and deep copy in Python

When the deepcopy() method is used, it indicates that any changes made to a copy of the object will not affect the original object.

While in the copy() method, any modifications to a copy of an object will reflect the original object.

Additionally, according to the official documentation of Python, the difference between shallow and deep copying is only relevant for compound objects (objects that contain other objects, like lists or class instances):

  • shallow copy constructs a new compound object and then (to the extent possible) inserts references into it to the objects found in the original.
  • deep copy constructs a new compound object and then, recursively, inserts copies into it of the objects found in the original.

Below is the difference between shallow copy and deep copy in Python.

How to Shallow Copy in Dictionary in Python?

original_dict = {1:"Prince", 2:"Grace", 3:["Gboi","Jei","Von"]}  # the original copy
new = original_dict.copy()  # this is the declaration of the copy

print("The original:",original_dict)
print("The new copy:",new)

# let's modify the value of key 3 of the copy object
new[3][2] = "PnG"
print("\nAfter modification")
print("The original:",original_dict)  # this prints the original object
print("The new copy:",new)  # this prints the copy object

Output:

The original: {1: 'Prince', 2: 'Grace', 3: ['Gboi', 'Jei', 'Von']}
The new copy: {1: 'Prince', 2: 'Grace', 3: ['Gboi', 'Jei', 'Von']}

After modification
The original: {1: 'Prince', 2: 'Grace', 3: ['Gboi', 'Jei', 'PnG']}
The new copy: {1: 'Prince', 2: 'Grace', 3: ['Gboi', 'Jei', 'PnG']}
Python Shallow Copy

How to Deep Copy in Dictionary in Python?

import copy

original_dict = {1:"Prince", 2:"Grace", 3:["Gboi","Jei","Von"]}  # the original copy
new = copy.deepcopy(original_dict)  # this is the declaration of the deep copy

print("The original:",original_dict)
print("The new copy:",new)

# let's modify the value of key 3 of the copy object
new[3][2] = "PnG"
print("\nAfter modification of new object")
print("The original:",original_dict)  # this prints the original object
print("The new copy:",new)  # this prints the copy object

Output:

The original: {1: 'Prince', 2: 'Grace', 3: ['Gboi', 'Jei', 'Von']}
The new copy: {1: 'Prince', 2: 'Grace', 3: ['Gboi', 'Jei', 'Von']}

After modification of new object
The original: {1: 'Prince', 2: 'Grace', 3: ['Gboi', 'Jei', 'Von']}
The new copy: {1: 'Prince', 2: 'Grace', 3: ['Gboi', 'Jei', 'PnG']}
Python Deep Copy

Note that Python deepcopy() is a member of copy module. To use the deep copy in Python, we have to import the copy module before using it.

Summary

In this Python tutorial, we learned about the different ways to copy a dictionary in Python. We also learned the differences between shallow copy and deep copy.

Leave a Comment