Python Copy List Tutorial

In this section, we will learn how to copy a list in Python.

Python Copy List

In Python we can copy the elements of a list and create another one as a result!

There are multiple ways to do this! For example, we can use the `list()` function, `copy()` method, which is a method that belongs to the Python List object. Or we can use the `deepcopy()` method, which is another method that is coming from the Python list object.

When want to run a copy operation, there are two concepts that you need to be aware of because they affect how the copy operation will be done!

One is `shallow copy` and the other is `deep copy`.

Let’s go into the details and see what shallow and deep copy mean in Python.

Python Types of Copy: Shallow Copy vs Deep Copy

As mentioned before, there are two methods of copying elements in Python:

– Shallow Copy:

In shallow copy, the elements of a list are copied from one list to another, but with one exception! If an element of the list that we want to copy it, is itself another list, then only the memory address of that list will be passed to the new list! That means both the new and old lists are pointing to the same sub-list and if one of them starts to change the elements of this sub-list, the other can see the change. (Note that this only affects the elements that are modifiable objects like a sub-list or a dictionary etc.! But if the elements are basic types like integer, double, string etc. then a copy of the target element will be made in a way that the new list will have its own independent value! So any change the new list runs on such elements, the old list won’t see).

We can run shallow copy using `list()` function as well as `copy()` method.

– Deep Copy:

On the other hand, we have the deep copy! This method of copying the elements happens in a way that the new list and the old list each will have their own completely independent elements no-matter if the element is of basic types or modifiable objects like a sub-list, dictionary, etc.

We can run a deep copy in Python using deepcopy() method.

Python Shallow Copy: List copy() Method

Alright, the first method is called `copy()`. Using this method, we can run a shallow copy on a list.

Python List copy() Method Syntax

list.copy()

Note that the method does not take any argument.

Example: python copy a list

list1 = [1,2,3,4,{"name":"John"},9,10]

shallow = list1.copy()

shallow[4]["name"] = "Jack"

print(list1[4])

Output:

{'name': 'Jack'}

Note that the fifth element in the `list1` object is a dictionary! This means the object is a modifiable one. So what happens here is that only a pointer from this object will be passed to the new list! This means both the old and the new list both have the same connection to the dictionary object and if one of them changed a property in this dictionary, the other will see the change and that’s what happened in this example.

Python list() function:

Another method of copying the elements of one list to another is by using the `list()` function.

This function takes one argument, and that is an iterable object (including another list object) as its argument and it will create a shallow copy of that list.

Example: using list() function in Python

list1 = [1,2,3,4,[5,6,7,8],9,10]

shallow = list(list1)

shallow[4][0] = 0

shallow[4][1] = 0

shallow[4][2] = 0

print(list1[4])

Output:

[0, 0, 0, 8]

Python Deep Copy: deepcopy() Method

Now if we want to run a deep copy operation on a list so that the entire elements of the target list get copied from one list to another with no link and relation in between, we can use the `deepcopy()` method.

Note that this method belongs to the `copy` module and we need to import that module first in order to be able to work with the method.

Python deepcopy() Method Syntax:

import copy

copy.deepcopy(targetList)

The method takes one argument and that is the target list that we want to deep copy its elements.

Example: using deepcopy() method in python

import copy

list1 = [1,2,3,4,{"name":"John"},9,10]

shallow = copy.deepcopy(list1)

shallow[4]["name"] = "Jack"

print(list1[4])

Output:

{'name': 'John'}

Passing a List Variable to another Variable

Note that passing a variable that is pointing to another variable is not a copy operation!

What happens here is that only a pointer will be passed to the new variable! That means both the old and new variables point to the same list and no copy will happen.

Example: passing a list variable to another variable in Python

list1 = [1,2,3,4,5,6]

list2 = list1

list2[0] = 1000

print(list1[0])

Output:

1000

As you can see, using the `list2` variable, we’ve changed the first element of the target list, but then using the `list1` variable, we could see this change too. This proves that both `list1` and `list2` are pointing to the same list object.

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies