Skip to content

Instantly share code, notes, and snippets.

@mh0w
Created October 21, 2022 15:35
Show Gist options
  • Save mh0w/17b8bccfbdc7acb90bec748cd8ab5181 to your computer and use it in GitHub Desktop.
Save mh0w/17b8bccfbdc7acb90bec748cd8ab5181 to your computer and use it in GitHub Desktop.
copying lists with and without linking them
# Here, updating l2 will also update l
l = [1,2,3]
l2 = l
l2[0] += 1
print(l) # [2,2,3]
print(l2) # [2,2,3]
# Here, updating l2 will not update l
l = [1,2,3]
l2 = l.copy()
l2[0] += 1
print(l) # [1,2,3]
print(l2) # [2,2,3]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment