On Thu, Jun 6, 2019 at 5:40 PM Yanghao Hua firstname.lastname@example.org wrote:
On Thu, Jun 6, 2019 at 6:31 AM Ryan Gonzalez email@example.com wrote:
Think of it more like indexing a range. Say you have:
L[:] = M[:]
Which is the same as:
L[0:len(L)] = M[0:len(M)]
Which mentally you can think of like:
L, L,...L[len(L)] = M,M,...M[len(M)]
Slicing is just indexing that represents more than one element, and if you think about it like that slice assignment makes much more sense.
This makes definitely sense on its own right. Problem is, when it moves to the right hand side, it means exactly the opposite: instead of indexing the full range of L, it is now representing a copy of L (X = L[:]).
Come again? It's still indexing the full range of L - that's not "exactly the opposite"!
So L[:] = thing vs thing = L[:], although in both assignment L shows up exactly the same: L[:], but means completely different things.
They have a difference for the built-in list type in that slicing a list returns a new list with references to the same objects, thus "x = x[:]" is going to give you an equivalent but distinct list. That's an important point in some contexts, but it's by no means "completely different".