Skip to content

Instantly share code, notes, and snippets.

@marquesghm
Created April 26, 2018 16:53
Show Gist options
  • Save marquesghm/7e44f6ea65a15e301e3e6c882fd5f8cd to your computer and use it in GitHub Desktop.
Save marquesghm/7e44f6ea65a15e301e3e6c882fd5f8cd to your computer and use it in GitHub Desktop.
Python List - Copy
import copy
print("Copy List Reference:")
a = [1, 2, 3]
b = a
b[0] = 42
print('{}\n{}'.format(a, b))
print("Copy List Elements")
a = [1, 2, 3]
b = a.copy()
# or b = copy.copy()
b[0] = 42
print('{}\n{}'.format(a, b))
print("Copy List Objects")
a = [1, 2, 3, [4, 5, 6]]
b = copy.deepcopy(a)
b[3][1] = 42
print('{}\n{}'.format(a, b))
print("Copy List Objects (Slicing). Obs: Didn't copy objects! =(")
a = [1, 2, 3, [4, 5, 6]]
b = a[:]
b[3][1] = 42
print('{}\n{}'.format(a, b))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment