Created
October 21, 2022 15:35
-
-
Save mh0w/17b8bccfbdc7acb90bec748cd8ab5181 to your computer and use it in GitHub Desktop.
copying lists with and without linking them
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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