• Courses
  • Tutorials
  • DSA
  • Data Science
  • Web Tech
October 20, 2022 |1.1K Views

Python program to Clone or Copy a list

  Share   Like
Description
Discussion

In this video, we will learn how to Clone or Copy a list in Python programming

We have discussed 8 different approaches to copy or clone a list. 
1. Slicing
2. Extend
3. Assignment operator
4. Shallow copy
5. List Comprehension
6. Append
7. Copy
8. Deep Copy

Approach 1: List Slicing: In this approach we use the concept of list slicing to copy one list into another list. This is the easiest and fastest way of copying a list.

Approach 2: Using list extend() method: In this approach we create a new list, and add all the elements of the existing list into the new list using list extend() method.

Approach 3: Using assignment operator: In this approach, we simply can create a new copy list from the existing list using assignment operator. But in this approach, the new list created shares the reference of the existing list. Thus, either of the list is changed, it will be reflected in the other list.

Approach 4: Using copy.copy() function: In this method, we create a shallow copy of the list.

Approach 5: List comprehension: In this method, we create a new list from the existing list using list comprehension.

Approach 6: Using list append() method: In this approach, we create a new list and iterate on the elements of the existing list and append each item into the new list.

Approach 7: Using list copy() method: In this method, we create a copy list using .copy() method of Python list. For nested elements, any changes made in either of the lists, reflects in the other list.

Approach 8: Using deepcopy() function from copy module: In this method, we can create a copy of a list. This is however the slowest process of copying a list, but in this process, changing the nested elements also doesn't affect the other list.

Cloning or copying a list: 
https://www.geeksforgeeks.org/python-cloning-copying-list/