Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python
shallow
copy
communitycreator

How to copy a dictionary in Python

Sarah Tanveer

Dictionaries are useful data structures frequently used in Python programming. There are several ways to make copies of dictionaries.

1. Use a for loop

A for loop can be used to iterate through the original dictionary and copy the elements into a new, empty dictionary one by one.

With this method, a shallow copy is produced. If you change this copy of the dictionary, it will not change the original dictionary. However, if one of the elements is an iterable object and a change is made to that element in the shallow copy, the same change will be reflected in the original copy.

# delcaring a dictionary
originalDict = {1: "A", 2: "B", 3: ["a", "b"]}

# declaring a new, empty dictionary
shallowCopy = {}

print("Original Dictionary:", originalDict)

# creating a shallow copy using for loop
for key, value in originalDict.items():
  shallowCopy[key] = value

print("Shallow copy: ", shallowCopy)

# adding an element to the shallow copy
shallowCopy[4] = "D"

# printing the shallow copy after adding an element
print("Shallow copy after adding an element: ", shallowCopy)

# printing the original dictionary to show no changes took place
print("Original dictionary remains the same: ", originalDict)

# however, making a change in the 3rd value of the shallow copy
# which is a list, will result in the original dictionary being changed
shallowCopy[3].append("hello")

print("Shallow Copy after changing list element:", shallowCopy)
print("Original Dictionary also changes: ", originalDict)

2. Use copy()

This is a built-in Python function that can be used to create a shallow copy of a dictionary.

dictionary.copy()

This function takes no arguments and returns a shallow copy of the dictionary. When a change is made to the shallow copy, the original dictionary will remain unchanged. Like the for loop method, the exception of the iterable object is present here.

# delcaring a dictionary
originalDict = {1: "Lahore", 2: "Islamabad", 3: "Karachi"}

print("Original Dictionary:", originalDict)

# creating a shallow copy using function
shallowCopy = originalDict.copy()
print("Shallow copy: ", shallowCopy)

# adding an element to the shallow copy
shallowCopy[4] = "Quetta"

# printing the shallow copy after adding an element
print("Shallow copy after appending an element: ", shallowCopy)

# printing the original dictionary to show no changes took place
print("Original dictionary remains the same: ", originalDict)

3. Use dict()

The built-in dict() function can be used to make a shallow copy of a dictionary.

dict(originalDict)

This function will take the dictionary to be copied as an argument and return a shallow copy of that dictionary. WHen a change is made to the shallow copy, the original remains unchanged, unless a change is made to an iterable element.

# delcaring a dictionary
originalDict = {1: "Lahore", 2: "Islamabad", 3: "Karachi"}

print("Original Dictionary:", originalDict)

# creating a shallow copy using function
shallowCopy = dict(originalDict)
print("Shallow copy: ", shallowCopy)

# adding an element to the shallow copy
shallowCopy[4] = "Quetta"

# printing the shallow copy after adding an element
print("Shallow copy after appending an element: ", shallowCopy)

# printing the original dictionary to show no changes took place
print("Original dictionary remains the same: ", originalDict)

RELATED TAGS

python
shallow
copy
communitycreator
RELATED COURSES

View all Courses

Keep Exploring