** * This article is from Udemy "[Introduction to Python3 taught by active Silicon Valley engineers + application + American Silicon Valley style code style](https://www.udemy.com/course/python-beginner/" Introduction to Python3 taught by active Silicon Valley engineers + application + American Silicon Valley style code style ")" It is a class notebook for myself after taking the course of. It is open to the public with permission from the instructor Jun Sakai. ** **
copy_dict
x = {'a': 1}
y = x
y['a'] = 1000
print('x = ', x, type(x), id(x))
print('y = ', y, type(y), id(y))
result
x = {'a': 1000} <class 'dict'> 4486017904
y = {'a': 1000} <class 'dict'> 4486017904
Copying a dictionary is similar to copying a list.
copy_dict
x = {'a': 1}
y = x.copy()
y['a'] = 1000
print('x = ', x, type(x), id(x))
print('y = ', y, type(y), id(y))
result
x = {'a': 1} <class 'dict'> 4342547312
y = {'a': 1000} <class 'dict'> 4342547392
By using .copy
, the influence on x can be avoided.
[Introduction to Udemy Python3 + Application] 19. Copy of list
Recommended Posts