Does a slicing operation give me a deep or shallow copy? Does a slicing operation give me a deep or shallow copy? python python

Does a slicing operation give me a deep or shallow copy?


You are creating a shallow copy, because nested values are not copied, merely referenced. A deep copy would create copies of the values referenced by the list too.

Demo:

>>> lst = [{}]>>> lst_copy = lst[:]>>> lst_copy[0]['foo'] = 'bar'>>> lst_copy.append(42)>>> lst[{'foo': 'bar'}]>>> id(lst) == id(lst_copy)False>>> id(lst[0]) == id(lst_copy[0])True

Here the nested dictionary is not copied; it is merely referenced by both lists. The new element 42 is not shared.

Remember that everything in Python is an object, and names and list elements are merely references to those objects. A copy of a list creates a new outer list, but the new list merely receives references to the exact same objects.

A proper deep copy creates new copies of each and every object contained in the list, recursively:

>>> from copy import deepcopy>>> lst_deepcopy = deepcopy(lst)>>> id(lst_deepcopy[0]) == id(lst[0])False


You should know that tests using is or id can be misleading of whether a true copy is being made with immutable and interned objects such as strings, integers and tuples that contain immutables.

Consider an easily understood example of interned strings:

>>> l1=['one']>>> l2=['one']>>> l1 is l2False>>> l1[0] is l2[0]True

Now make a shallow copy of l1 and test the immutable string:

>>> l3=l1[:]>>> l3 is l1False>>> l3[0] is l1[0]True

Now make a copy of the string contained by l1[0]:

>>> s1=l1[0][:]>>> s1'one'>>> s1 is l1[0] is l2[0] is l3[0]True               # they are all the same object

Try a deepcopy where every element should be copied:

>>> from copy import deepcopy>>> l4=deepcopy(l1)>>> l4[0] is l1[0]True

In each case, the string 'one' is being interned into Python's internal cache of immutable strings and is will show that they are the same (they have the same id). It is implementation and version dependent of what gets interned and when it does, so you cannot depend on it. It can be a substantial memory and performance enhancement.

You can force an example that does not get interned instantly:

>>> s2=''.join(c for c in 'one')>>> s2==l1[0]True>>> s2 is l1[0]False

And then you can use the Python intern function to cause that string to refer to the cached object if found:

>>> l1[0] is s2False>>> s2=intern(s2)>>> l1[0] is s2True

Same applies to tuples of immutables:

>>> t1=('one','two')>>> t2=t1[:]>>> t1 is t2True>>> t3=deepcopy(t1)>>> t3 is t2 is t1True

And mutable lists of immutables (like integers) can have the list members interred:

>>> li1=[1,2,3]>>> li2=deepcopy(li1)>>> li2 == li1True>>> li2 is li1False>>> li1[0] is li2[0]True

So you may use python operations that you KNOW will copy something but the end result is another reference to an interned immutable object. The is test is only a dispositive test of a copy being made IF the items are mutable.