How to clone or copy a list in Python?


The list in Python is a sequence data type which is used to store various types of data. A list is created by placing each data element inside square brackets [] and these are separated by commas.

In Python, assignment operator doesn’t create a new object, rather it gives another name to already existing object. This can be verified by id() function

>>> L1 = [1,2,3,4]
>>> L2 = L1
>>> id(L1)
185117137928
>>> id(L2)
185117137928

There are various ways of cloning/copying a list in python. In this article, we will discuss some of them.

Using the assignment operator

The simplest way of cloning a list is assigning an old list object to the new object. Here we need to use the assignment operator “=”.

Syntax

Old_list = new_list

Example

The assignment operator doesn’t create a new list object, rather it gives another name to an already existing object. As we can see in the above example the ID of both objects are the same which is verified by id() function.

If we make changes in any of the lists the other one also gets changed, so if we want to keep the original list unchanged, the following methods can be used.

l1 = [1,2,3,4]
l2 = l1
print("Original List:", l1)
print("After Cloning:", l2)

print("ID of Original list", id(l1))
print("ID of copied list", id(l2))

Output

Original List: [1, 2, 3, 4]
After Cloning: [1, 2, 3, 4]
ID of Original list 140565795507408
ID of copied list 140565795507408

Using list slicing technique

Slicing technique in python is used to access a range of items in a list. This technique can also be used for cloning a list, where we want to modify a list and also keep a copy of the original.

Syntax

list_obj[start:stop:step]

Example

From the above example, it has been proven that the slicing technique of list can also be used for cloning a list.

l1 = [1,2,3,4]
l2= l1
print("Original List:", l1)
print("After Cloning:", l2)

print("ID of Original list", id(l1))
print("ID of copied list", id(l2))

l2.append(10)
print('Original list',l1)
print('Copied and updated list',l2)

Output

Original List: [1, 2, 3, 4]
After Cloning: [1, 2, 3, 4]

ID of Original list 140565661890112
ID of copied list 140565795507408

Original list [1, 2, 3, 4]
Copied and updated list [1, 2, 3, 4, 10]

Using copy() method

The copy() is a Python list method which is used to get a shallow copy of the list. It means if we do any modification of the new list and those changes will not be reflected on the original list.

Syntax

new_list = list.copy()

Example

The list.copy() method also successfully cloned a list. The cloned list object “l2” is created with the id 140565662101664.

l1 = [1,2,3,4]
l2 = l1.copy()
print("Original List:", l1)
print("After Cloning:", l2)

print("ID of Original list", id(l1))
print("ID of copied list", id(l2))

l2.append(10)
print('Original list',l1)
print('Copied and updated list',l2)

Output

Original List: [1, 2, 3, 4]
After Cloning: [1, 2, 3, 4]
ID of Original list 140565661966976
ID of copied list 140565662101664
Original list [1, 2, 3, 4]
Copied and updated list [1, 2, 3, 4, 10]

Using the list() method

The list() method is also considered as the simplest way of cloning a list. This function creates a new list object. Let’s take an example and see how the list() method clones a python list.

Example

l1 = [1,2,3,4]
# clone a list
l2 = list(l1)
print("Original List:", l1)
print("After Cloning:", l2)

print("ID of Original list", id(l1))
print("ID of copied list", id(l2))

l2.append(10)
print('Original list',l1)
print('Copied and updated list',l2)

Output

Original List: [1, 2, 3, 4]
After Cloning: [1, 2, 3, 4]
ID of Original list 140565661915808
ID of copied list 140565661713424
Original list [1, 2, 3, 4]
Copied and updated list [1, 2, 3, 4, 10]	

All these are some of the different ways to clone a python list. Also we can use list comprehension, extend(), and append() methods to clone a list.

Updated on: 24-Aug-2023

604 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements