DEV Community

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

Posted on • Edited on

Shallow Copy & Deep Copy in Python (6)

Buy Me a Coffee

*Memos:

  • My post explains an iterator (1).
  • 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 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 dictionary.
  • My post explains variable assignment.

<Normal Copy>:

*Memos:

  • v1 and v2 refer to the same shallow and deep iterator.
  • is keyword can check if v1 and v2 refer to the same iterator.
 # Shallow iterator # ↓↓↓↓↓↓ ↓↓ v1 = iter([iter(['a'])]) v2 = v1 # ↑↑↑↑↑↑↑↑↑↑↑  # Deep iterator print(v1) # <list_iterator object at 0x0000029DDFFB7D30> print(v2) # <list_iterator object at 0x0000029DDFFB7D30> print(v1 is v2) # True  print(next(v1)) # <list_iterator object at 0x0000029DDEC54E50> print(next(v2)) # StopIteration: 
Enter fullscreen mode Exit fullscreen mode

<Shallow Copy>:

copy() can do shallow copy as shown below:

*Memos:

  • v1 and v2 refer to different shallow iterators.
  • v1 and v2 refer to the same deep iterator.
from copy import copy v1 = iter([iter(['a'])]) v2 = copy(v1) # Here  print(v1) # <list_iterator object at 0x0000029DE015CDC0> print(v2) # <list_iterator object at 0x0000029DE015F340> print(v1 is v2) # False  v3 = next(v1) v4 = next(v2) print(v3) # <list_iterator object at 0x0000029DD4BF82E0> print(v4) # <list_iterator object at 0x0000029DD4BF82E0> print(v3 is v4) # True  print(next(v3)) # a print(next(v4)) # StopIteration: 
Enter fullscreen mode Exit fullscreen mode

<Deep Copy>:

deepcopy() can do deep copy as shown below:

*Memos:

  • v1 and v2 refer to the different shallow and deep iterators.
  • deepcopy() should be used because it's safe, doing copy deeply.
from copy import deepcopy v1 = iter([iter(['a'])]) v2 = deepcopy(v1) # Here  print(v1) # <list_iterator object at 0x0000029DDF283E80> print(v2) # <list_iterator object at 0x0000029DDF282080> print(v1 is v2) # False  v3 = next(v1) v4 = next(v2) print(v3) # <list_iterator object at 0x0000029DDF279D50> print(v4) # <list_iterator object at 0x0000029DDF283FA0> print(v3 is v4) # False  print(next(v3)) # a print(next(v4)) # a 
Enter fullscreen mode Exit fullscreen mode

The below with copy() which can do shallow copy is equivalent to the above:

from copy import copy v1 = iter([iter(['a'])]) v2 = copy(v1) # Here  print(v1) # <list_iterator object at 0x0000029DDFF4D7E0> print(v2) # <list_iterator object at 0x0000029DDFF4D2D0> print(v1 is v2) # False  v3 = copy(next(v1)) # Here v4 = copy(next(v2)) # Here  print(v3) # <list_iterator object at 0x0000029DDFF4CEE0> print(v4) # <list_iterator object at 0x0000029DDF283E80> print(v3 is v4) # False  print(next(v3)) # a print(next(v4)) # a 
Enter fullscreen mode Exit fullscreen mode

Additionally, the below is a 3D iterator:

from copy import deepcopy v1 = iter([iter([iter(['a'])])]) v2 = deepcopy(v1) # Here  print(v1) # <list_iterator object at 0x0000029DDF281EA0> print(v2) # <list_iterator object at 0x0000029DDEAECB20> print(v1 is v2) # False  v3 = next(v1) v4 = next(v2) print(v3) # <list_iterator object at 0x0000029DDF282FE0> print(v4) # <list_iterator object at 0x0000029DDFF4F760> print(v3 is v4) # False  v5 = next(v3) v6 = next(v4) print(v5) # <list_iterator object at 0x0000029DDEECEA40> print(v6) # <list_iterator object at 0x0000029DDFF4FAC0> print(v5 is v6) # False  print(next(v5)) # a print(next(v6)) # a 
Enter fullscreen mode Exit fullscreen mode

Top comments (0)