Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python list slice syntax used for no obvious reason

I occasionally see the list slice syntax used in Python code like this:

newList = oldList[:] 

Surely this is just the same as:

newList = oldList 

Or am I missing something?

like image 751
Charles Anderson Avatar asked Nov 27 '08 12:11

Charles Anderson


People also ask

How do you slice a list in Python?

The format for list slicing is [start:stop:step]. start is the index of the list where slicing starts. stop is the index of the list where slicing ends. step allows you to select nth item within the range start to stop.

Which is true about slicing of lists in Python?

With this operator, one can specify where to start the slicing, where to end, and specify the step. List slicing returns a new list from the existing list. If Lst is a list, then the above expression returns the portion of the list from index Initial to index End, at a step size IndexJump.

What is the syntax for slicing?

Python slice() Function Syntax: start: Starting index where the slicing of object starts. stop: Ending index where the slicing of object stops. step: It is an optional argument that determines the increment between each index for slicing.

Is list slicing inclusive Python?

Python is a zero-indexed language (things start counting from zero), and is also left inclusive, right exclusive you are when specifying a range of values. This applies to objects like lists and Series , where the first element has a position (index) of 0.


1 Answers

[:] 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 = data  aa = 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 elements NewList = 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]._data print OldList[0]._data 

Running it in a python shell gives the following transcript. We can see the list being made with copies of the old objects. One of the objects can have its state updated by reference through the old list, and the updates can be seen when the object is accessed through the old list. Finally, changing a reference in the new list can be seen to not reflect in the old list, as the new 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]._data aaa >>> >>> # The shallow copy makes a new list pointing to the old elements ... NewList = OldList[:] >>> print NewList[0]._data aaa >>> >>> # 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 xxx >>> >>> # Updating the new list to point to something new is not reflected ... # in the old list. ... NewList[0] = Foo ('ccc') >>> print NewList[0]._data ccc >>> print OldList[0]._data xxx 
like image 148
ConcernedOfTunbridgeWells Avatar answered Oct 06 '22 07:10

ConcernedOfTunbridgeWells