
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Clone or Copy a List in Python
The list in Python is a sequence data type that 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, the assignment operator doesn't create a new object; rather, it gives another name to an 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.
Cloning a List Using Assignment Operator
The simplest way of cloning a list is by assigning an old list object to the new object. Here we need to use the assignment operator "=".
Syntax
Following is the syntax to copy the list using the assignment operator in Python -
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 IDs of both objects are the same, which is verified by the 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))
Following is an output of the above code -
Original List: [1, 2, 3, 4] After Cloning: [1, 2, 3, 4] ID of Original list 140565795507408 ID of copied list 140565795507408
Cloning a List using List slicing
The 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
Following is the syntax to copy the list using list slicing in Python -
list_obj[start:stop:step]
Example
From the above example, it has been proven that the slicing technique of a 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)
Following is an output of the above code -
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]
Cloning a List using copy() Method
The copy() is a Python list method that is used to get a shallow copy of the list. It means if we do any modification of the new list, those changes will not be reflected in the original list.
Syntax
Following is the syntax to copy a list using the copy() method in Python -
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)
Following is an output of the above code -
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]
Cloning a List using the list() Method
The list() method is also considered 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
Here, we have copied the list, l1 to the list l2 using list() function -
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)
Following is an output of the above code -
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.