Shallow and Deep Copying

We saw earlier (on page 16) that if we have two variables referring to the same string and we change one of them, for example using += to append—Python creates a new string. This occurs because Python strings are immutable. For mutable types such as lists (and dictionaries, covered shortly), the situation is different.

For example, if we create a list with two variables referring to it, and we change the list through one of the variables, both variables now refer to the same changed list:

>>> seaweed = ["Aonori", "Carola", "Dulse"] >>> macroalgae = seaweed >>> seaweed, macroalgae

(['Aonori', 'Carola', 'Dulse'], ['Aonori', 'Carola', 'Dulse']) >>> macroalgae[2] = "Hijiki" >>> seaweed, macroalgae

(['Aonori', 'Carola', 'Hijiki'], ['Aonori', 'Carola', 'Hijiki'])

This is because by default, Python uses shallow copying when copying mutable data. We can force Python to do a deep copy by taking a slice that consists of the entire list:

>>> seaweed = ["Aonori", "Carola", "Dulse"] >>> macroalgae = seaweed[:] >>> seaweed, macroalgae

(['Aonori', 'Carola', 'Dulse'], ['Aonori', 'Carola', 'Dulse']) >>> macroalgae[2] = "Hijiki" >>> seaweed, macroalgae

(['Aonori', 'Carola', 'Dulse'], ['Aonori', 'Carola', 'Hijiki'])

Slices always copy the items sliced, whether we slice a part of a list, or the whole list as we have done here. However, this works only one level deep, so if we had a list of lists, the sublists would only be shallow-copied. Some other collection types—for example, dict—provide a copy() method which is their equivalent of [:] .

For deep copying that works to any depth we must import the copy module and use the deepcopy() function. In practice though, this is very rarely a problem, and when it does trip us up, using deepcopy() sorts it out for us.

We have replaced a slice of length one, fruit[2:3] ("Loquat"), with a slice of length two. We have also inserted a slice of three items without removing any. In the last example we copied all of fruit's items to bag; this could have been done using bag = fruit, but with subtly different semantics; see the Shallow and Deep Copying sidebar for more about copying lists.

Multiple consecutive items can be deleted using del on a slice, or by assigning a zero-length slice to a slice. To insert multiple items we can use slicing, or we can slice with operator +, and to add at the end we can use extend(). See Table 1.4 for a summary of the methods and functions applicable to lists.

Was this article helpful?

0 0


The main focus of this report is to show how to get involved in video marketing on the run, how to rank quickly on YouTube and Google using FREE semi-automatic tools and services. QUICKLY AND FREE. I will show methods and techniques I use to rank my videos, as well as free resources and tools to make video clips, to get backlinks and free traffic.

Get My Free Ebook

Post a comment