DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on • Edited on

Shallow Copy & Deep Copy in Python (3)

Buy Me a Coffee

*Memo:

  • My post explains the shallow and deep copy of a list.
  • My post explains the shallow and deep copy of a tuple.
  • My post explains the shallow and deep copy of the set with an iterator.
  • My post explains the shallow and deep copy of a dictionary.
  • My post explains the shallow and deep copy of an iterator.
  • My post explains a set and the set with copy.

<A set with a tuple>

A set can have the hashable types of elements like frozenset, tuple, iterator, etc but cannot have the unhashable types of elements like set, list, dict, etc.

*Memo:

  • The same tuple is always referred to because a tuple cannot be copied according to the experiments so for the set with a tuple, only shallow copy is possible.

<Normal Copy>:

*Memo:

  • A and B refer to the same shallow set and deep tuple.
  • is keyword can check if v1 and v2 refer to the same set or tuple.
# Shallow set
#   ↓      ↓ 
A = {('a',)}
   # ↑↑↑↑↑↑ Deep tuple
B = A

print(A) # {('a',)}
print(B) # {('a',)}

print(A is B)
# True

print(A.pop()) # ('a',)
print(B.pop()) # KeyError: 'pop from an empty set'
Enter fullscreen mode Exit fullscreen mode

<Shallow Copy>:

set.copy() does shallow copy as shown below:

*Memo:

  • A and B refer to different shallow sets.
  • A and B refer to the same deep tuple.
A = {('a',)}
B = A.copy()

print(A) # {('a',)}
print(B) # {('a',)}

print(A is B)
# False

v1 = A.pop()
v2 = B.pop()

print(v1) # ('a',)
print(v2) # ('a',)

print(v1 is v2)
# True
Enter fullscreen mode Exit fullscreen mode

The below with copy.copy() which does shallow copy is equivalent to the above:

import copy

A = {('a',)}
B = copy.copy(A)

print(A) # {('a',)}
print(B) # {('a',)}

print(A is B)
# False

v1 = A.pop()
v2 = B.pop()

print(v1) # ('a',)
print(v2) # ('a',)

print(v1 is v2)
# True
Enter fullscreen mode Exit fullscreen mode

The below with set() which does shallow copy is equivalent to the above:

A = {('a',)}
B = set(A)

print(A) # {('a',)}
print(B) # {('a',)}

print(A is B)
# False

v1 = A.pop()
v2 = B.pop()

print(v1) # ('a',)
print(v2) # ('a',)

print(v1 is v2)
# True
Enter fullscreen mode Exit fullscreen mode

The below with deepcopy() which does deep copy is equivalent to the above:

*Memo:

  • copy.deepcopy() should be used because it's safe, doing copy deeply while set.copy(), copy.copy() and set() aren't safe, doing copy shallowly.
import copy

A = {('a',)}
B = copy.deepcopy(A)

print(A) # {('a',)}
print(B) # {('a',)}

print(A is B)
# False

v1 = A.pop()
v2 = B.pop()

print(v1) # ('a',)
print(v2) # ('a',)

print(v1 is v2)
# True
Enter fullscreen mode Exit fullscreen mode

Additionally, the below is the 3D set with a 2D tuple:

import copy 

A = {(('a',),)}
B = copy.deepcopy(A)

print(A) # {(('a',),)}
print(B) # {(('a',),)}

print(A is B)
# False

v1 = A.pop()
v2 = B.pop()

print(v1) # (('a',),)
print(v2) # (('a',),)

print(v1[0]) # ('a',)
print(v2[0]) # ('a',)

print(v1 is v2, v1[0] is v2[0])
# True True
Enter fullscreen mode Exit fullscreen mode

Top comments (0)