s[:] = s[::-1]
versus
s = s[::-1]
Please explain the difference between achieving the result using the above methods.
s[:] = s[::-1]
versus
s = s[::-1]
Please explain the difference between achieving the result using the above methods.
The first mutates the original list itself. The second creates a new list, and assigns it back to the variable.
>>> s = [1,2,3]
>>> id(s)
2186036088200 # original id
>>> s[:] = s[::-1]
>>> id(s)
2186036088200 # still same id
>>> s = s[::-1]
>>> id(s)
2186036085000 # new id, because new list was created
The first method is called "slice assignment".