Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (47.6k points)

While reading up the documentation for dict.copy(), it says that it makes a shallow copy of the dictionary. Same goes for the book I am following (Beazley's Python Reference), which says:

The m.copy() method makes a shallow copy of the items contained in a mapping object and places them in a new mapping object.

Consider this:

>>> original = dict(a=1, b=2)

>>> new = original.copy()

>>> new.update({'c': 3})

>>> original {'a': 1, 'b': 2}

>>> new {'a': 1, 'c': 3, 'b': 2}

So I assumed this would update the value of original (and add 'c': 3) also since I was doing a shallow copy. Like if you do it for a list:

>>> original = [1, 2, 3]

>>> new = original

>>> new.append(4)

>>> new, original

([1, 2, 3, 4], [1, 2, 3, 4])

This works as expected.

Since both are shallow copies, why is that the dict.copy() doesn't work as I expect it to? Or my understanding of shallow vs deep copying is flawed?

1 Answer

0 votes
by (106k points)

When you use the "shallow copying" it means the content of the dictionary is not copied by value, but just creating a new reference.

a = {1: [1,2,3]}

b = a.copy()

print(a, b) 

a[1].append(4)

print(a, b) 

image

When you use a deep copy it copies all contents by value.

import copy

c = copy.deepcopy(a)

print(a, c) 

a[1].append(5)

print(a, c) 

image

Browse Categories

...