Python list slice syntax used for no obvious reason Python list slice syntax used for no obvious reason python python

Python list slice syntax used for no obvious reason


[:] Shallow copies the list, making a copy of the list structure containing references to the original list members. This means that operations on the copy do not affect the structure of the original. However, if you do something to the list members, both lists still refer to them, so the updates will show up if the members are accessed through the original.

A Deep Copy would make copies of all the list members as well.

The code snippet below shows a shallow copy in action.

# ================================================================# === ShallowCopy.py =============================================# ================================================================#class Foo:    def __init__(self, data):        self._data = dataaa = Foo ('aaa')bb = Foo ('bbb')# The initial list has two elements containing 'aaa' and 'bbb'OldList = [aa,bb]print OldList[0]._data# The shallow copy makes a new list pointing to the old elementsNewList = OldList[:]print NewList[0]._data# Updating one of the elements through the new list sees the# change reflected when you access that element through the# old list.NewList[0]._data = 'xxx'print OldList[0]._data# Updating the new list to point to something new is not reflected# in the old list.NewList[0] = Foo ('ccc')print NewList[0]._dataprint OldList[0]._data

Running it in a python shell gives the following transcript. We can see thelist being made with copies of the old objects. One of the objects can haveits state updated by reference through the old list, and the updates can beseen when the object is accessed through the old list. Finally, changing areference in the new list can be seen to not reflect in the old list, as thenew list is now referring to a different object.

>>> # ================================================================... # === ShallowCopy.py =============================================... # ================================================================... #... class Foo:...     def __init__(self, data):...         self._data = data...>>> aa = Foo ('aaa')>>> bb = Foo ('bbb')>>>>>> # The initial list has two elements containing 'aaa' and 'bbb'... OldList = [aa,bb]>>> print OldList[0]._dataaaa>>>>>> # The shallow copy makes a new list pointing to the old elements... NewList = OldList[:]>>> print NewList[0]._dataaaa>>>>>> # Updating one of the elements through the new list sees the... # change reflected when you access that element through the... # old list.... NewList[0]._data = 'xxx'>>> print OldList[0]._dataxxx>>>>>> # Updating the new list to point to something new is not reflected... # in the old list.... NewList[0] = Foo ('ccc')>>> print NewList[0]._dataccc>>> print OldList[0]._dataxxx


Like NXC said, Python variable names actually point to an object, and not a specific spot in memory.

newList = oldList would create two different variables that point to the same object, therefore, changing oldList would also change newList.

However, when you do newList = oldList[:], it "slices" the list, and creates a new list. The default values for [:] are 0 and the end of the list, so it copies everything. Therefore, it creates a new list with all the data contained in the first one, but both can be altered without changing the other.


As it has already been answered, I'll simply add a simple demonstration:

>>> a = [1, 2, 3, 4]>>> b = a>>> c = a[:]>>> b[2] = 10>>> c[3] = 20>>> a[1, 2, 10, 4]>>> b[1, 2, 10, 4]>>> c[1, 2, 3, 20]