Friday, 4 December 2015

Python: list: Copy elements of list

List provides copy method, to get a shallow copy of the list.

test.py
list=[12, 5, 32, 43, 21, 356, 5, 21]
list1 = list.copy()

print(list)
print(list1)

list1.append(12345)

print(list)
print(list1)

$ python3 test.py
[12, 5, 32, 43, 21, 356, 5, 21]
[12, 5, 32, 43, 21, 356, 5, 21]
[12, 5, 32, 43, 21, 356, 5, 21]
[12, 5, 32, 43, 21, 356, 5, 21, 12345]



Previous                                                 Next                                                 Home

No comments:

Post a Comment