*Memo for shallow and deep copy:
- My post explains a list.
- My post explains a tuple.
- My post explains the set with a frozenset.
- My post explains the set with a tuple.
- My post explains the set with an iterator.
- My post explains a frozenset.
- My post explains an iterator.
- My post explains a string.
- My post explains a bytes.
- My post explains a bytearray.
- My post explains a range.
*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
andtuple
.
<Assignment>:
*Memo:
-
v1
andv2
refer to the same outer and inner dictionary. -
is
keyword can check ifv1
andv2
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'}}
# ↑↑↑ ↑↑↑
<Shallow copy>:
*Memo:
-
v1
andv2
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'}}
# ↑↑↑ ↑↑↑
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'}}
# ↑↑↑ ↑↑↑
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'}}
# ↑↑↑ ↑↑↑
<Deep copy>:
*Memo:
-
v1
andv2
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 whiledict.copy()
,copy.copy()
anddict()
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'}}
# ↑↑↑ ↑↑↑
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'}}
# ↑↑↑ ↑↑↑
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'}}
# ↑↑↑ ↑↑↑
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'}}
# ↑↑↑ ↑↑↑
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'}}}
# ↑↑↑ ↑↑↑ ↑↑↑
Top comments (0)