Shallow copy & Deep copy in Python (11)

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 a frozenset.

My post explains the shallow and deep co…


This content originally appeared on DEV Community and was authored by Super Kai (Kazuya Ito)

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 a frozenset.
  • My post explains the shallow and deep copy of the set with 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 frozenset.
  • 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 the shallow and deep copy of an string.
  • My post explains a bytes.

Different bytearrays are referred to if copied.

A 2D bytearray is experimented, doing assignment and shallow and deep copy as shown below:

*Memo:

  • For a 2D bytearray, only shallow copy is possible.
  • A bytearray has 2 demesions(D).

<Assignment>:

*Memo:

  • v1 and v2 refer to the same shallow and deep bytearray.
  • is keyword can check if v1 and v2 refer to the same bytearray.
     # Shallow bytearray
#    ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓ 
v1 = bytearray(b'abcde')
v2 = v1        # ↑↑↑↑↑ Deep bytearray(Each byte)

print(v1) # bytearray(b'abcde')
print(v2) # bytearray(b'abcde')

print(v1 is v2, v1[2] is v2[2])
# True True True True

v2[1] = ord('X')
v2[3] = ord('Y')

print(v1) # bytearray(b'aXcYe')
print(v2) # bytearray(b'aXcYe')

<Shallow copy>:

*Memo:

  • v1 and v2 refer to different shallow bytearrays.
  • v1 and v2 refer to the same deep bytearray.

bytearray.copy() shallow-copies the 2D bytearray as shown below:

v1 = bytearray(b'abcde')
v2 = v1.copy()

print(v1) # bytearray(b'abcde')
print(v2) # bytearray(b'abcde')

print(v1 is v2, v1[2] is v2[2])
# False True

v2[1]= ord('X')
v2[3]= ord('Y')
          #              ↓ ↓
print(v1) # bytearray(b'abcde')
print(v2) # bytearray(b'aXcYe')
          #              ↑ ↑

copy.copy() shallow-copies a 2D bytearray as shown below:

import copy

v1 = bytearray(b'abcde')
v2 = copy.copy(v1)

print(v1) # bytearray(b'abcde')
print(v2) # bytearray(b'abcde')

print(v1 is v2, v1[2] is v2[2])
# False True

v2[1]= ord('X')
v2[3]= ord('Y')
          #              ↓ ↓
print(v1) # bytearray(b'abcde')
print(v2) # bytearray(b'aXcYe')
          #              ↑ ↑

Slicing shallow-copies the 2D bytearray as shown below:

v1 = bytearray(b'abcde')
v2 = v1[:]

print(v1) # bytearray(b'abcde')
print(v2) # bytearray(b'abcde')

print(v1 is v2, v1[2] is v2[2])
# False True

v2[1]= ord('X')
v2[3]= ord('Y')
          #              ↓ ↓
print(v1) # bytearray(b'abcde')
print(v2) # bytearray(b'aXcYe')
          #              ↑ ↑

bytearray() shallow-copies a 2D bytearray as shown below:

v1 = bytearray(b'abcde')
v2 = bytearray(v1)

print(v1) # bytearray(b'abcde')
print(v2) # bytearray(b'abcde')

print(v1 is v2, v1[2] is v2[2])
# False True

v2[1]= ord('X')
v2[3]= ord('Y')
          #              ↓ ↓
print(v1) # bytearray(b'abcde')
print(v2) # bytearray(b'aXcYe')
          #              ↑ ↑

<Deep copy>:

*Memo:

  • v1 and v2 refer to different shallow bytearrays.
  • v1 and v2 refer to the same deep bytearray.

copy.deepcopy() doesn't deep-copy a 2D bytearray as shown below:

import copy

v1 = bytearray(b'abcde')
v2 = copy.deepcopy(v1)

print(v1) # bytearray(b'abcde')
print(v2) # bytearray(b'abcde')

print(v1 is v2, v1[2] is v2[2])
# False True

v2[1]= ord('X')
v2[3]= ord('Y')
          #              ↓ ↓
print(v1) # bytearray(b'abcde')
print(v2) # bytearray(b'aXcYe')
          #              ↑ ↑


This content originally appeared on DEV Community and was authored by Super Kai (Kazuya Ito)


Print Share Comment Cite Upload Translate Updates
APA

Super Kai (Kazuya Ito) | Sciencx (2025-09-30T18:46:27+00:00) Shallow copy & Deep copy in Python (11). Retrieved from https://www.scien.cx/2025/09/30/shallow-copy-deep-copy-in-python-11/

MLA
" » Shallow copy & Deep copy in Python (11)." Super Kai (Kazuya Ito) | Sciencx - Tuesday September 30, 2025, https://www.scien.cx/2025/09/30/shallow-copy-deep-copy-in-python-11/
HARVARD
Super Kai (Kazuya Ito) | Sciencx Tuesday September 30, 2025 » Shallow copy & Deep copy in Python (11)., viewed ,<https://www.scien.cx/2025/09/30/shallow-copy-deep-copy-in-python-11/>
VANCOUVER
Super Kai (Kazuya Ito) | Sciencx - » Shallow copy & Deep copy in Python (11). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/09/30/shallow-copy-deep-copy-in-python-11/
CHICAGO
" » Shallow copy & Deep copy in Python (11)." Super Kai (Kazuya Ito) | Sciencx - Accessed . https://www.scien.cx/2025/09/30/shallow-copy-deep-copy-in-python-11/
IEEE
" » Shallow copy & Deep copy in Python (11)." Super Kai (Kazuya Ito) | Sciencx [Online]. Available: https://www.scien.cx/2025/09/30/shallow-copy-deep-copy-in-python-11/. [Accessed: ]
rf:citation
» Shallow copy & Deep copy in Python (11) | Super Kai (Kazuya Ito) | Sciencx | https://www.scien.cx/2025/09/30/shallow-copy-deep-copy-in-python-11/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.