list we can copy by using slice operator ,the element of the first list should same in the next list but when you change the element from the cloned list that elements only changes previous list should same like before.
x=[10,20,30,40]
y=x[:]
print(x)
y[1]=111
print(id(x))
print(id(y))
print(y)
print(x)
output:
[10, 20, 30, 40]
2576163316104
2576163860680
[10, 111, 30, 40]
[10, 20, 30, 40]
x=[10,20,30,40]
y=x[:]
print(x)
y[1]=111
print(id(x))
print(id(y))
print(y)
print(x)
output:
[10, 20, 30, 40]
2576163316104
2576163860680
[10, 111, 30, 40]
[10, 20, 30, 40]
No comments:
Post a Comment