+ 2
What is the difference between list and list[:] in python?
https://code.sololearn.com/cH9QLZPtElbl/?ref=app Why outputs are note the same for the two lists b and c ?
2 Answers
+ 3
Try the following:
a = [1, 2, 3]
b = a
print(b == a)
print(b is a)
# theyâre the same object
# they are identical
c = a[:]
print(c == a)
print(c is a)
# they have the same values
# they are equal
That means that changing b will also change a, while c is a different (yet equal) object.
+ 1
Even if b is just a copy of a, the operation b[1][1]=6 have affected a but b[0]=6 doesn't.