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

*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 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 an iterator (1).

<Normal Copy>:

*Memo:

  • 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.copy() does shallow copy as shown below:

*Memo:

  • v1 and v2 refer to different shallow iterators.
  • v1 and v2 refer to the same deep iterator.
import copy v1 = iter([iter(['a'])]) v2 = copy.copy(v1) 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

iter() doesn't do shallow copy as shown below:

v1 = iter([iter(['a'])]) v2 = iter(v1) print(v1) # <list_iterator object at 0x00000192769592D0> print(v2) # <list_iterator object at 0x00000192769592D0>  print(v1 is v2) # True  print(next(v1)) # <list_iterator object at 0x000001927695B4F0> print(next(v2)) # StopIteration: 
Enter fullscreen mode Exit fullscreen mode

<Deep Copy>:

deepcopy() does deep copy as shown below:

*Memo:

  • v1 and v2 refer to the different shallow and deep iterators.
  • copy.deepcopy() should be used because it's safe, doing copy deeply while copy.copy() isn't safe, doing copy shallowly.
import copy v1 = iter([iter(['a'])]) v2 = copy.deepcopy(v1) 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.copy() does shallow copy is equivalent to the above:

import copy v1 = iter([iter(['a'])]) v2 = copy.copy(v1) print(v1) # <list_iterator object at 0x0000029DDFF4D7E0> print(v2) # <list_iterator object at 0x0000029DDFF4D2D0>  print(v1 is v2) # False  v3 = copy(next(v1)) v4 = copy(next(v2)) 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:

import copy v1 = iter([iter([iter(['a'])])]) v2 = copy.deepcopy(v1) 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)