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
Tube Jacker

Tube Jacker

Download Tube Jacker And Discover Everything You Need To Know About Jacking Unlimited Traffic From The Video Giant. The drop dead easy way to create winning video campaigns that will FLOOD your website with unstoppable FREE traffic, all on complete and total autopilot. How to exploit a sneaky method of boosting exposure and getting your videos to the top of Google within 72 hours, guaranteed.

Get My Free Ebook


Post a comment