What is the difference between list and list[:] in python? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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 ?

28th May 2018, 5:48 PM
Ahmed Errami
Ahmed Errami - avatar
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.
28th May 2018, 7:07 PM
Pedro Demingos
Pedro Demingos - avatar
+ 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.
28th May 2018, 6:02 PM
Ahmed Errami
Ahmed Errami - avatar