Skip to content

Instantly share code, notes, and snippets.

@oakbani
Created April 26, 2019 05:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oakbani/c93d6b53d8b5fe9e833317e106588c5b to your computer and use it in GitHub Desktop.
Save oakbani/c93d6b53d8b5fe9e833317e106588c5b to your computer and use it in GitHub Desktop.
# Python List makes a shallow copy by default and doesn't copy-on-write.
list_a = [1,2,3]
list_shallow = list_a
list_shallow.append(4)
print(list_a) # [1, 2, 3, 4]
list_a.append(5)
print(list_shallow) # [1, 2, 3, 4, 5]
list_a = [1,2,3]
list_deep = list_a[:]
list_deep.append(4)
print(list_a) # [1, 2, 3]
list_a.append(5)
print(list_deep) # [1, 2, 3, 4]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment