Copy or clone a list in Python

Introduction

Copying data structures such as lists is a frequently performed task in computer programming. For instance, one of the most efficient sorting algorithms is merge sort. Merge sort uses divide and conquer approach to split the unsorted list into two sections in a recursive fashion. To do so, merge sort uses list copying technique. You may check the following post for more information.

Getting back to the main topic, we are going to summarize different ways to copy lists in Python. Let us get started…

List assignment

Before we dive into our topic, let us clarify an import point. In Python, assigning a list to another one does not perform any copying. Assignment only copies the reference. The end result is having two pointers to the same data. If we modify one, the other is modified automatically. The id built in function can be used to prove that both lists have the same id. Actual copying produces distict ids. Here is an example…

If you run the code snippet above, you should get the following output…

As you can see, both A and B have the same id meaning they both point to the same memory location.

List slicing

Slicing in Python is handy when it comes to extracting data from lists. Using slicing technique to clone a list is a little bit odd but it does the job perfectly. Slicing creates a new list and copies elements from the source list. Here is an example…

If you run the code snippet above, you should get two different lists with distinct ids…

Note that slicing performs shallow copying. In other words, if the source list contains objects then only the references to those objects are copied. Here is an example…

Here is the output for the above code snippet…

List construction

A list object in Python can be constructed from any other object of sequence type. This means we can clone an existing list using the list constructor. Here is an example…

Construction also works with generators. Here is an example…

If you run the code snippet above, you should get the following output…

For more information about generators, you may check the following post.

Copy module

Python offers a dedicated module for copying. In the next two sections we will see how to perform shallow and deep copying using the copy module…

Shallow copy

Using the same example mentioned earlier but using the copy.copy() method…

We should get the same output we got earlier using slicing…

Deep copy

If the list contains objects, deepcopy should be used. Here is an example…

If you run the code snippet above, you should get the following output…

Copy method

In Python 3.x, the list data structure has a shallow copy method. This is not supported in Python 2.x. Here is an example…

If you run the code snippet above, you should get the following output…

extend function

Extending a list can also be used to copy another list. For more information about extend, you may check the following post. Let us take an example…

If you run the code snippet above, you should get the following output…

List comprehension

List comprehension can also be used to copy lists. For more information about list comprehension, you may check the following post. Let us take an example…

If you run the code snippet above, you should get the following output…

Manual copying using append

Appending to an existing list can be used to copy data from another list. Here is an example…

If you run the code snippet above, you should get the following output…

Time to summarize…

Summary

  • There are various ways to clone a list in Python. Which method to use depends on preference, performance or convenience
  • Shallow copying copies elements and references to nested structures
  • Deep copying copies all elements including nested data structures
  • List assignment copies only the reference
  • Lists in Python can be copied using slicing, construction, copy module, copy list method
  • Other methods to copy lists include extending a list, list comprehension or appending elements

References

Thanks for visiting. For feedback, please use the comments section below.

Tags:

Add a Comment

Your email address will not be published. Required fields are marked *