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

*Memos:

  • 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 variable assignment.

<A set with a tuple>

A set can have a tuple and iterator but cannot have a set, list and dictionary. *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:

*Memos:

  • 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:

copy() can do shallow copy as shown below:

*Memos:

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

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() which can do shallow copy is equivalent to the above:

from copy import copy

A = {('a',)}
B = copy(A) # Here

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 can do deep copy is equivalent to the above. *deepcopy() should be used because it's safe, doing copy deeply:

from copy import deepcopy

A = {('a',)}
B = deepcopy(A) # Here

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 can do shallow copy is equivalent to the above:

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

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:

from copy import deepcopy

A = {(('a',),)}
B = deepcopy(A) # Here

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)