slice - Python : Why use "list[:]" when "list" refers to same thing? -
consider list >>> l=[1,2,3].
what benefit of using >>> l[:] when >>> l prints same thing former does?
thanks.
it creates (shallow) copy.
>>> l = [1,2,3] >>> m = l[:] >>> n = l >>> l.append(4) >>> m [1, 2, 3] >>> n [1, 2, 3, 4] >>> n l true >>> m l false
Comments
Post a Comment