Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

The official Python docs say that using the slicing operator and assigning in Python makes a shallow copy of the sliced list.

But when I write code for example:

o = [1, 2, 4, 5]
p = o[:]

And when I write:

id(o)
id(p)

I get different id's and also appending one one list does not reflect in the other list. Isn't it creating a deep copy or is there somewhere I am going wrong?

like image 377
user2528042 Avatar asked Sep 28 '13 15:09

user2528042


People also ask

Does slicing make deep copy?

slice() , Array. from() , Object. assign() , and Object. create() ) do not create deep copies (instead, they create shallow copies).

Is slicing a deep copy Python?

The official Python docs say that using the slicing operator and assigning in Python makes a shallow copy of the sliced list.

Does Python slicing create a copy?

No, slicing returns a list which is inside the original list. Not a copied list. But, in your code; there is a function which edits the parameter-list. If you are working with parameters, you must know that changes made at a parameter doesn't effect the original variable you pass into it.

What is deep copy and shallow copy?

Deep copy stores copies of the object's value. Shallow Copy reflects changes made to the new/copied object in the original object. Deep copy doesn't reflect changes made to the new/copied object in the original object. Shallow Copy stores the copy of the original object and points the references to the objects.

What is slicing operator How can you use it?

The slice operator [n:m] returns the part of the string from the n'th character to the m'th character, including the first but excluding the last. In other words, start with the character at index n and go up to but do not include the character at index m.

What is deep and shallow copy in Python?

A shallow copy constructs a new compound object and then (to the extent possible) inserts references into it to the objects found in the original. A deep copy constructs a new compound object and then, recursively, inserts copies into it of the objects found in the original.


2 Answers

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
like image 138
Martijn Pieters Avatar answered Sep 18 '22 16:09

Martijn Pieters


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 l2
False
>>> l1[0] is l2[0]
True

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

>>> l3=l1[:]
>>> l3 is l1
False
>>> 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 s2
False
>>> s2=intern(s2)
>>> l1[0] is s2
True

Same applies to tuples of immutables:

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

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

>>> li1=[1,2,3]
>>> li2=deepcopy(li1)
>>> li2 == li1
True
>>> li2 is li1
False
>>> 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.

like image 40
dawg Avatar answered Sep 18 '22 16:09

dawg