Ways to Copy a List in Python

Ways To Copy A Python List

In this article, we will be understanding various techniques to copy a list in Python.

Python List is a data structure to store and manipulate the data values.


Technique 1: The extend() method to copy a list in Python

Python built-in extend() method can be used to copy the elements of a list to another list.

The extend() method basically takes an iterable as argument say list, tuple, dict, etc and it iterates or traverses through the elements of the iterable and adds the elements to the new list in an element-by-element fashion.

Syntax:

list.iterable(iterable)

Example:

list1 = [10, 20, 30, 40, 50, 60, 70, 80, 90] 
copy_list = []
copy_list.extend(list1)
print("Input List:", list1) 
print("Copied List:", copy_list) 

Output:

Input List: [10, 20, 30, 40, 50, 60, 70, 80, 90]
Copied List: [10, 20, 30, 40, 50, 60, 70, 80, 90]

Technique 2: The slicing operator to copy a list in Python

Python slicing operator is considered to be the most efficient way to copy elements of a Python list.

Syntax:

[start:stop:steps]
  • start: It determines the start of slicing.
  • stop: This parameter determines the end of the slicing of iterable
  • steps: It determines the number of elements to be skipped or the intervals at which slicing has to be performed.

In the above, in order to copy the list, we have used slicing in the following format:

[:]

which simply means that the slicing of the list would begin at the start index i.e. index 0 and would end at the last element with step value = 1.

Example:

list1 = [10, 20, 30, 40, 50, 60, 70, 80, 90] 
copy_list = []
copy_list = list1[:]
print("Input List:", list1) 
print("Copied List:", copy_list) 

Output:

Input List: [10, 20, 30, 40, 50, 60, 70, 80, 90]
Copied List: [10, 20, 30, 40, 50, 60, 70, 80, 90]


Technique 3: List Comprehension to copy a list in Python

Python List Comprehension technique is useful to copy a list in Python. It is just an alternative way of creating a statement in just a single line of code

Syntax:

[element for element in list]

Example:

list1 = [10, 20, 30, 40, 50, 60, 70, 80, 90] 
copy_list = []
copy_list = [item for item in list1]
print("Input List:", list1) 
print("Copied List:", copy_list) 

In the above snippet of code, we have used List Comprehension, wherein the ‘item’ acts as a pointer element and traverses through the list ‘list1’ and copies the data values in an element-by-element fashion.

Output:

Input List: [10, 20, 30, 40, 50, 60, 70, 80, 90]
Copied List: [10, 20, 30, 40, 50, 60, 70, 80, 90]


Technique 4: The list() method to copy a list

Python list() method basically accepts an iterable as an argument and returns the sequence as a list i.e. converts the iterable to a List.

Syntax:

list([iterable])

In the below piece of code, we pass a list-list1 to the list() method, so as to create a new list with all the elements of list-list1 and thus, serve the purpose of copying a list.

Example:

list1 = [10, 20, 30, 40, 50, 60, 70, 80, 90] 
copy_list = []
copy_list = list(list1)
print("Input List:", list1) 
print("Copied List:", copy_list) 

Output:

Input List: [10, 20, 30, 40, 50, 60, 70, 80, 90]
Copied List: [10, 20, 30, 40, 50, 60, 70, 80, 90]

Technique 5: Python copy() method to copy a list

Python in-built copy() method can be used to copy the data items of a list to another. The copy() method copies the elements of a list to another list in an element-by-element fashion by traversing the list.

Syntax:

list.copy()

Example:

list1 = [10, 20, 30, 40, 50, 60, 70, 80, 90] 
copy_list = []
copy_list = list1.copy()
print("Input List:", list1) 
print("Copied List:", copy_list) 

Output:

Input List: [10, 20, 30, 40, 50, 60, 70, 80, 90]
Copied List: [10, 20, 30, 40, 50, 60, 70, 80, 90]

Technique 6: The append() method to copy a Python list

Python in-built append() method can be easily used to copy the elements of a list to another list.

As the name suggests, the append() method appends .i.e. attaches the elements of the list to the end of the required list.

But because we’re working with an empty list, in this case, we can use this method to copy a list in Python.

Syntax:

list.append(value or element)

Example:

list1 = [10, 20, 30, 40, 50, 60, 70, 80, 90] 
copy_list = []
for ele in list1: copy_list.append(ele) 
print("Input List:", list1) 
print("Copied List:", copy_list) 

Output:

Input List: [10, 20, 30, 40, 50, 60, 70, 80, 90]
Copied List: [10, 20, 30, 40, 50, 60, 70, 80, 90]

Conclusion

Thus, we have unveiled different ways to copy a list in Python.

But, readers, it isn’t the end of learning, I strongly recommend everyone to refer the above examples and try to implement the same practically.


References

  • Python List