Shallow Copy & Deep Copy in Python (3)
Super Kai (Kazuya Ito)

Super Kai (Kazuya Ito) @hyperkai

About: I'm a web developer. Buy Me a Coffee: ko-fi.com/superkai SO: stackoverflow.com/users/3247006/super-kai-kazuya-ito X(Twitter): twitter.com/superkai_kazuya FB: facebook.com/superkai.kazuya

Joined:
Oct 21, 2021

Shallow Copy & Deep Copy in Python (3)

Publish Date: May 31
0 0

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 a set and the set with copy.

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

set.copy() does 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()

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. *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

Comments 0 total

    Add comment