Python lists are immutable, however, the slicing operation returns a new list every time. This is why a common idiom in python to get a new copy of a list is to slice it with [:]
. This returns a new list containing all the items of the old lost.
However, there is a subtle point about the mutability or immutability of the elements inside the list. In your case, since they are numbers and are immutable, the new list returned from slicing is an entirely new list and changing it will not affect the original list:
>>> my_list = [1, 2, 3] # Create a list with three numbers
>>> my_other_list = my_list[:] # Slice the original list to get a copy
>>> my_other_list[0] = 42 # Change the first element of the new list
>>> my_other_list, my_list # See the two lists side by side
([42, 2, 3], [1, 2, 3]) # The change to the new list did not effect the original list
But, if the content of the first list were of an mutable type, say a list
. Then the story would be different:
>>> my_list = [[1], [2], [3]]
>>> my_other_list = my_list[:]
>>> my_other_list[0].append(42)
>>> my_other_list, my_list
([[1, 42], [2], [3]], [[1, 42], [2], [3]])
Suffice it to say that in your case the elements of your list are numbers and they are immutable. So, every time you slice the original list, you are getting a new list and any changes you make to it has no effect on the original list.