examples/lists/not_copy.py
from __future__ import print_function x = ['apple', 'bob', 'cat', 'drone'] y = x x[0] = 'qqrq' print(x) # ['qqrq', 'bob', 'cat', 'drone'] print(y) # ['qqrq', 'bob', 'cat', 'drone']
- There is one list in the memory and two pointers to it.
- If you really want to make a copy the pythonic way is to use the slice syntax.
examples/lists/real_copy.py
from __future__ import print_function x = ['apple', 'bob', 'cat', 'drone'] y = x[:] x[0] = 'qqrq' print(x) # ['qqrq', 'bob', 'cat', 'drone'] print(y) # ['apple', 'bob', 'cat', 'drone']