Created
April 26, 2018 16:53
-
-
Save marquesghm/7e44f6ea65a15e301e3e6c882fd5f8cd to your computer and use it in GitHub Desktop.
Python List - Copy
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
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