Python: How to Copy a List? (The Idiomatic Way)

There are many ways to copy a python list.

But first, let’s talk about what copying a list actually means.

Let’s take a look at this example

>>> a = [1, 2, 3, 4, 5]
>>> b = a
>>> a
[1, 2, 3, 4, 5]
>>> b
[1, 2, 3, 4, 5]

In the example above, is b a copy of a?

The answer is actually No

b is in fact a, they both refer to the same python object.

Let’s check what happens to b when we modify a.

>>> a[0] = 10
>>> b
[10, 2, 3, 4, 5]

As you can see, changing the value of a[0] also changes the value of b[0] because they both refer to the same list object.

So what does copying a list mean?

Copying a python list means creating a new python object whose contents are identical.

The following figure shows what we want to achieve when we copy or clone a list.

In this article, we will discuss three different methods to copy a python list.

The first two methods can be used in python2 and python3 whereas the third one works for python3 only.

First: Copying by Slicing

The most common way (especially in python2) to copy a python list is to use slicing.

If you have been coding in python for a while, you probably came across some code that looks like this.

>>> b = a[:]

When you omit the start index and the end index from the slice, then your slice will start from the beginning of the list all the way to the end of the list.

And because slicing creates a new object, then the above code effectively copies or clones the whole list into another list.

Let’s go ahead and confirm this.

>>> a = [1, 2, 3, 7]
>>> b = a[:]
>>> b
[1, 2, 3, 7]
>>> id(a)
4440018888
>>> id(b)
4440454712

This code confirms two things:

1- the items of list b are the same as those of list a

2- a and b are different objects

But how do we know they are different?

One way is by observing that id(a) is different from id(b).

If you don’t know what the id() function does, it basically returns the address of a python object in the memory.

Needless to say, two variables will refer to the same object only if the id of these two variable are exactly the same. Otherwise, they refer to different objects.

You want to be more assured?

Let’s try to modify a and see if b remains unchanged.

If b is modified, then a and b refer to the same object.

If b remains unchanged, then a and b refer to two separate objects.

>>> a = [1, 2, 3, 7]
>>> b = a[:]
>>> b
[1, 2, 3, 7]
>>> a[0] = -10
>>> a
[-10, 2, 3, 7]
>>> b
[1, 2, 3, 7]

As you can see, after a was modified, b remains unchanged.

Awesome, we successfully copied a python list.

Second: Copying using list() function

Another way to create a copy of a list is to use the list() built-in function.

The list() function is used to create a list object from any iterable.

And most of the time in real code, this iterable is not a list.

For example, the following code creates a new list off of the items of a string.

>>> s = "hello"
>>> l = list(s)
>>> l
['h', 'e', 'l', 'l', 'o']

But since a list is an iterable itself, there is nothing that prevents you from creating a list from another list.

>>> a = [1, 2, 3, 4]
>>> b = list(a)
>>> b
[1, 2, 3, 4]
>>> id(a)
4354322312
>>> id(b)
4354377672

Even though this is not a common way to copy a list, it is still a valid one.

Third: Copying using the copy() Method

This is when Python3 comes to the rescue with a beautiful way to copy a list.

Python3 introduced a new method to lists called copy() and it does exactly what you think it does.

It copies a list into another list.

>>> a = [1, 2, 3, 4]
>>> b = a.copy()
>>> id(a)
4354356936
>>> id(b)
4354322312

The only downside is that it is not available in python2.

But if you are using python3, there is no debate this is the best, and most readable way to copy a list.

Conclusion

If you are using python2, you can copy a list by slicing or by using the list() function.

If you are using python3, use the list’s copy() method.

Learning Python?

If you are a beginner, then I highly recommend this book.

No longer a beginner?

Then you are ready for this book to get to the next level (It’s my favorite).

2018-11-19T01:15:58+00:00 By |

4 Comments

  1. Gaby October 23, 2018 at 9:07 pm - Reply

    Nice explanation! It was very useful.

  2. Nastya November 2, 2018 at 7:30 pm - Reply

    thanks

Leave A Comment