Skip to content
Home » Copying Lists – Shallow Copies in Python

Copying Lists – Shallow Copies in Python

Spread the love

Today we’ll be talking about copying lists, and in particular, about shallow copies, which are the default type of copies and the one you usually want. In the next video we’ll cover deep copies as well, so you’ll see what the difference is.

Here’s the video version of the article:

Before you start reading this article, maybe you would like to read my article How to Copy a List in Python in Three Different Ways, which is related to this one.

Shallow Copy

A shallow list is a list without a nested structure.

A shallow copy is a copy of a list in which sublists are not copied.

A deep copy is a copy of a list in which sublists are also copied.

In order to copy a list we can use the copy method:

>>> animals = ["pig", "snake", "fish"]
>>> animals_copy = animals.copy()
>>> animals_copy
['pig', 'snake', 'fish']

or the slice operator:

>>> animals = ["pig", "snake", "fish"]  
>>> animals_copy = animals[:]  # a shallow copy of the whole list

Either way, we get a shallow copy. If there are no sublists, the distinction between shallow copy and deep copy is irrelevant. In the example above we have such a list. After making the shallow copy the two lists are identical. Let’s change an element in the copy:

>>> animals_copy[1] = "shark"

As the two lists are separate objects, the change doesn’t affect the original list:

>>> animals
['pig', 'snake', 'fish']

It only affects the copy:

>>> animals_copy
['pig', 'shark', 'fish']

Now we’ll see how it works in case of lists with sublists:

>>> sports = ["soccer", "tennis", ["hammer throw", "javelin throw"]]
>>> sports_copy = sports[:]  # a shallow copy of the whole list

Your Panda3D Magazine

Make Awesome Games and Other 3D Apps

with Panda3D and Blender using Python.

Cool stuff, easy to follow articles.

Get the magazine here (PDF).

In a shallow copy the first two elements will be copied like before. Let’s change the first element:

>>> sports_copy[0] = "football"

>>> sports
['soccer', 'tennis', ['hammer throw', 'javelin throw']]

>>> sports_copy
['football', 'tennis', ['hammer throw', 'javelin throw']]

The sublist, which is the third element, will not be copied. Only the reference to the sublist will be copied, which means that both the original and the copy list will reference the same sublist. So, if we change an element in either of them, the change will be visible in both:

>>> sports_copy[2][1] = "discus throw"

>>> sports
['soccer', 'tennis', ['hammer throw', 'discus throw']]

>>> sports_copy
['football', 'tennis', ['hammer throw', 'discus throw']]

If this is not what you want, you need a deep copy. But this is a subject for another article, so stay tuned.

Python Jumpstart Course

Learn the basics of Python, including OOP.

with lots of exercises, easy to follow

The course is available on Udemy.

Blender Jumpstart Course

Learn the basics of 3D modeling in Blender.

step-by-step, easy to follow, visually rich

The course is available on Udemy and on Skillshare.


Spread the love

Leave a Reply