How To Copy List in Python

118

Lists in Python are one of the built-in data types you can use to store data collection. Read this Python tutorial to understand better how to work with lists in Python. In this tutorial, we will discuss how to copy lists in Python. We can use the Python List copy() method to copy a list to another list.

Before we move ahead and explore different methods to copy a list in Python, let’s first understand what a deep and shallow copy is.

Also ReadHow To Sort a List of Tuples in Python

Deep and Shallow Copy in Python

Whenever we use the assignment operator to copy the value of one variable to another, then Python performs a shallow copy as the new variable stores the reference to the first variable. Any value change in the original variable is automatically reflected in the new variable.

On the other hand, when we perform the deep copy in Python, it creates a constructor and then recursively stores the value of each item to the new variable. In this case, any changes in the original list aren’t reflected in the new list.

Now that you understand the concept of deep and shallow copy, let’s explore various methods to copy a list in Python.

Also ReadHow to Convert Python Tuples to Lists

How To Copy List in Python

We will cover 5 methods to copy a list in Python:

  • Copy() method
  • List() method
  • Assignment operator
  • List comprehension
  • Slicing syntax

Also ReadPython 3 Program To Find Largest Element In An Array or List

Method 1: Copy() method

Copy() method returns the shallow copy of the list. The method doesn’t take any parameters and returns a new list. Syntax of copy() method is: list2 = list1.copy().

Code:

studentAges = [32, 33, 25]
newStudentAges = studentAges.copy()
print('Copied List:', newStudentAges)

Output:

python copy method list

If you modify the values in the studentAges list in the above code, the newStudentAges list will not be modified.

Also Read10 Best Programming Apps To Learn Python

Method 2: List() method

The list () method is another way to copy a list in Python. The built-in method takes in one parameter called iterable, which can be a sequence such as a tuple or string.

Code:

#Defining a list
studentAges = [1,2,3]
#Copying list using assignment operator
newStudentAges = list(studentAges)
print ("This is the new copied list: ", newStudentAges)

Output:

list method to copy lists in python

Also ReadPython Program To Reverse A Number

Method 3: Assignment operator

We can also copy a list with the Python assignment operator (=). The only drawback is that it doesn’t perform a shallow copy, and any modifications in the original list will be reflected in the new list.

Code:

#Defining a list
studentAges = [1,2,3]
#Copying list using assignment operator
newStudentAges = studentAges
print ("This is the new list: ", newStudentAges)

Output:

copy list python using assignment operator

Also ReadPython Program to Find Sum of Digits [5 Methods]

Method 4: List Comprehension

Code:

oldlist = ['CodeItBro','Python']
newList = []

for i in oldlist:
 newList.append(i)

print('New List: ', newList)

Output:

copying list in python using list comprehension

Also ReadPython Program To Check If A Number Is a Perfect Square

Method 5: Slicing Syntax

Code:

studentAges = [12, 10, 21]

# Using slicing to copy a list
newStudentAges = studentAges[:]

# Adding an element to the new list
newStudentAges.append(23)

# Displaying new and old list
print('Old List:', studentAges)
print('New List:', newStudentAges)

Output:

python copy list using slicing syntax

Also ReadIncrement and Decrement Operators in Python

Summary

You can use any of the five methods discussed above to copy a list in Python. Based on your requirements, you can choose a way to perform deep or shallow copy to clone a list to another list.