DEV Community

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

Posted on • Edited on

Shallow copy & Deep copy in Python (7)

Buy Me a Coffee

*Memo for shallow and deep copy:

*Memo for others:

  • My post explains a dictionary (1).

Different dictionaries are referred to, shallow-copied and deep-copied.


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

*Memo:

  • A 2D dictionary can be shallow-copied and deep-copied.
  • There are an assignment and 2 kinds of copies, shallow copy and deep copy:
    • An assignment is to create the one or more references to the original top level object and (optional) original lower levels' objects, keeping the same values as before.
    • A shallow copy is to create the one or more references to the new top level object and (optional) original lower levels' objects, keeping the same values as before.
    • A deep copy is to create the two or more references to the new top level object and the new lower levels' objects which you desire but at least the new 2nd level objects, keeping the same values as before:
      • A deep copy is the multiple recursions of a shallow copy so a deep copy can be done with multiple shallow copies.
    • Basically, immutable(hashable) objects aren't copied to save memory like str, bytes, int, float, complex, bool and tuple.

<Assignment>:

*Memo:

  • v1 and v2 refer to the same outer and inner dictionary.
  • is keyword can check if v1 and v2 refer to the same outer and/or inner dictionary.

A 2D dictionary is assigned to a variable without copied as shown below:

     ### Outer dictionary ###
#    ↓↓↓↓↓↓↓↓↓↓↓↓↓↓         ↓
v1 = {'a':'b', 'c':{'d':'e'}}
v2 = v1          # ↑↑↑↑↑↑↑↑↑ Inner dictionary

print(v1) # {'a': 'b', 'c': {'d': 'e'}}
print(v2) # {'a': 'b', 'c': {'d': 'e'}}

print(v1 is v2, v1['c'] is v2['c'])
# True True

v2['a'] = 'X'
v2['c']['d'] = 'Y'
          #       ↓↓↓             ↓↓↓
print(v1) # {'a': 'X', 'c': {'d': 'Y'}}
print(v2) # {'a': 'X', 'c': {'d': 'Y'}}
          #       ↑↑↑             ↑↑↑
Enter fullscreen mode Exit fullscreen mode

<Shallow copy>:

*Memo:

  • v1 and v2 refer to different outer dictionaries and the same inner dictionary.

dict.copy() can shallow-copy the 2D dictionary as shown below:

v1 = {'a':'b', 'c':{'d':'e'}}
v2 = v1.copy()

print(v1) # {'a': 'b', 'c': {'d': 'e'}}
print(v2) # {'a': 'b', 'c': {'d': 'e'}}

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

v2['a'] = 'X'
v2['c']['d'] = 'Y'
          #       ↓↓↓             ↓↓↓
print(v1) # {'a': 'b', 'c': {'d': 'Y'}}
print(v2) # {'a': 'X', 'c': {'d': 'Y'}}
          #       ↑↑↑             ↑↑↑
Enter fullscreen mode Exit fullscreen mode

copy.copy() can shallow-copy a 2D dictionary as shown below:

import copy

v1 = {'a':'b', 'c':{'d':'e'}}
v2 = copy.copy(v1)

print(v1) # {'a': 'b', 'c': {'d': 'e'}}
print(v2) # {'a': 'b', 'c': {'d': 'e'}}

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

v2['a'] = 'X'
v2['c']['d'] = 'Y'
          #       ↓↓↓             ↓↓↓
print(v1) # {'a': 'b', 'c': {'d': 'Y'}}
print(v2) # {'a': 'X', 'c': {'d': 'Y'}}
          #       ↑↑↑             ↑↑↑
Enter fullscreen mode Exit fullscreen mode

dict() can shallow-copy a 2D dictionary as shown below:

v1 = {'a':'b', 'c':{'d':'e'}}
v2 = dict(v1)

print(v1) # {'a': 'b', 'c': {'d': 'e'}}
print(v2) # {'a': 'b', 'c': {'d': 'e'}}

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

v2['a'] = 'X'
v2['c']['d'] = 'Y'
          #       ↓↓↓             ↓↓↓
print(v1) # {'a': 'b', 'c': {'d': 'Y'}}
print(v2) # {'a': 'X', 'c': {'d': 'Y'}}
          #       ↑↑↑             ↑↑↑
Enter fullscreen mode Exit fullscreen mode

<Deep copy>:

*Memo:

  • v1 and v2 refer to different outer and inner dictionaries.

copy.deepcopy() can deep-copy a 2D dictionary as shown below:

*Memo:

  • copy.deepcopy() should be used because it's safe, deeply copying a 2D dictionary while dict.copy(), copy.copy() and dict() aren't safe, shallowly copying a 2D dictionary.
import copy

v1 = {'a':'b', 'c':{'d':'e'}}
v2 = copy.deepcopy(v1)

print(v1) # {'a': 'b', 'c': {'d': 'e'}}
print(v2) # {'a': 'b', 'c': {'d': 'e'}}

print(v1 is v2, v1['c'] is v2['c'])
# False False

v2['a'] = 'X'
v2['c']['d'] = 'Y'
          #       ↓↓↓             ↓↓↓
print(v1) # {'a': 'b', 'c': {'d': 'e'}}
print(v2) # {'a': 'X', 'c': {'d': 'Y'}}
          #       ↑↑↑             ↑↑↑
Enter fullscreen mode Exit fullscreen mode

dict.copy() can deep-copy the 2D dictionary, shallow-copying the outer and inner dictionary as shown below:

v1 = {'a':'b', 'c':{'d':'e'}}
v2 = v1.copy()
v2['c'] = v1['c'].copy()

print(v1) # {'a': 'b', 'c': {'d': 'e'}}
print(v2) # {'a': 'b', 'c': {'d': 'e'}}

print(v1 is v2, v1['c'] is v2['c'])
# False False

v2['a'] = 'X'
v2['c']['d'] = 'Y'
          #       ↓↓↓             ↓↓↓
print(v1) # {'a': 'b', 'c': {'d': 'e'}}
print(v2) # {'a': 'X', 'c': {'d': 'Y'}}
          #       ↑↑↑             ↑↑↑
Enter fullscreen mode Exit fullscreen mode

copy.copy() can deep-copy a 2D dictionary, shallow-copying the outer and inner dictionary as shown below:

import copy

v1 = {'a':'b', 'c':{'d':'e'}}
v2 = copy.copy(v1)
v2['c'] = copy.copy(v1['c'])

print(v1) # {'a': 'b', 'c': {'d': 'e'}}
print(v2) # {'a': 'b', 'c': {'d': 'e'}}

print(v1 is v2, v1['c'] is v2['c'])
# False False

v2['a'] = 'X'
v2['c']['d'] = 'Y'
          #       ↓↓↓             ↓↓↓
print(v1) # {'a': 'b', 'c': {'d': 'e'}}
print(v2) # {'a': 'X', 'c': {'d': 'Y'}}
          #       ↑↑↑             ↑↑↑
Enter fullscreen mode Exit fullscreen mode

dict() can deep-copy a 2D dictionary, shallow-copying the outer and inner dictionary as shown below:

v1 = {'a':'b', 'c':{'d':'e'}}
v2 = dict(v1)
v2['c'] = dict(v1['c'])

print(v1) # {'a': 'b', 'c': {'d': 'e'}}
print(v2) # {'a': 'b', 'c': {'d': 'e'}}

print(v1 is v2, v1['c'] is v2['c'])
# False False

v2['a'] = 'X'
v2['c']['d'] = 'Y'
          #       ↓↓↓             ↓↓↓
print(v1) # {'a': 'b', 'c': {'d': 'e'}}
print(v2) # {'a': 'X', 'c': {'d': 'Y'}}
          #       ↑↑↑             ↑↑↑
Enter fullscreen mode Exit fullscreen mode

Additionally, copy.deepcopy() can deep-copy a 3D dictionary as shown below:

import copy

v1 = {'a':'b', 'c':{'d':'e', 'f':{'g':'h'}}}
v2 = copy.deepcopy(v1)

print(v1) # {'a': 'b', 'c': {'d': 'e', 'f': {'g': 'h'}}}
print(v2) # {'a': 'b', 'c': {'d': 'e', 'f': {'g': 'h'}}}

print(v1 is v2, v1['c'] is v2['c'], v1['c']['f'] is v2['c']['f'])
# False False False

v2['a'] = 'X'
v2['c']['d'] = 'Y'
v2['c']['f']['g'] = 'Z'
          #       ↓↓↓             ↓↓↓             ↓↓↓
print(v1) # {'a': 'b', 'c': {'d': 'e', 'f': {'g': 'h'}}}
print(v2) # {'a': 'X', 'c': {'d': 'Y', 'f': {'g': 'Z'}}}
          #       ↑↑↑             ↑↑↑             ↑↑↑
Enter fullscreen mode Exit fullscreen mode

Top comments (0)