You are currently viewing Python List copy() Method

The List copy() method creates a shallow copy of the list and returns the copied list. This method does not take any parameters and returns a new list object. A shallow copy means that a new list is created with the same elements as the original list, but the elements themselves are not copied. Instead, the new list contains references to the same objects as the original list. So any changes to the copied list also reflect on the original list.

Advertisements

In this article, I will explain the Python list copy() method syntax, parameters, and usage how to copy all the elements from a given list with examples.

1. Quick Examples of List copy() Method

If you are in a hurry, below are some quick examples of the list copy() method.


# Quick examples of list copy() method

# Initialize list
mylist = ["Python","Spark", "Hadoop","Java","C++"]

# Example 1: Copying a list
# Using the copy() method
copied_list = mylist.copy()

# Example 2: Copy list using = operator
copied_list = mylist
# Add an element to list
mylist.append("Pysork")

# Example 3: Using the slice operator
copied_list = mylist[:]

# Example 4: Using the slice operator [:]
copied_list = mylist[1:3]

# Example 5: let's modify the nested list 
# In the copied_list
mylist = [1, 2, 3, [4, 5]]
copied_list = mylist[:]
copied_list[3].append(6)

# Example 6: Create a copy of the list 
# Using the list() constructor
copied_list = list(mylist)

# Example 7: Copying a list 
# Using deepcopy() method
mylist = [1, 2, [3, 4], 5]
copied_list = copy.deepcopy(mylist)
copied_list[2].append(6)

2. Python List copy() Method

The copy() method is a built-in method for Python lists that creates a shallow copy of the list and returns the copied list. This method does not take any arguments and returns a new list object that contains references to the same objects as the original list.

2.1 Syntax of copy() Method

Following is the syntax of the list copy() method.


# Syntax of copy() function
list.copy()

2.1 Parameter of copy()

The copy() method does not take any parameter.

2.2 Return Value

It returns a new list object that contains the same elements as the original list, and does not modify the original list itself.

3. Copying a List Using the copy() Method

First, define a list called mylist with five elements. You then create a new list called copied_list by calling the copy() method on mylist. Finally, print both lists to confirm that they contain the same elements.


# Initialize list
mylist = ["Python", "Spark", "Hadoop","Java","C++"]
print("Original list: ", mylist)

# Copying a list
# Using the copy() method
copied_list = mylist.copy()
print("Copied the list: ", copied_list)

Yields below output.

python list copy

4. Copying a List Using = Operator

You can also use the = operator to copy a list, which does not actually create a new list. Instead, it creates a reference to the mylist, which means that changes made to the mylist will also be reflected in the copied list.


# Initialize list
mylist = ["Python","Spark", "Hadoop","Java","C++"]
# Copy list using =
copied_list = mylist

# Add an element to list
mylist.append("Pysork")
print("Original list: ", mylist)
print("Copied the list: ", copied_list)

Yields below output.

5. Other ways to Copy the List in Python

In this section let’s see other ways to copy the list.

5.1 Copying a List Using the Slice Operator

You can also create a copy of a list using the slice operator [:]. For example, you first define a list called mylist with five elements. You then create a new list called copied_list by using the slice operator [:] on mylist. Finally, you print both lists to confirm that they contain the same elements.


# Initialize list
mylist = ["Python", "Spark", "Hadoop","Java","C++"]
print("Original list: ", mylist)

# Using the slice operator
copied_list = mylist[:]
print("Copied the list: ", copied_list)

# Output:
# Original list:  ['Python', 'Spark', 'Hadoop', 'Java', 'C++']
# Copied the list:  ['Python', 'Spark', 'Hadoop', 'Java', 'C++']

Similar to using copy(), using the slice operator [:] creates a shallow copy of the list, which means that changes made to the objects within the list will affect both the original list and the copied list. It’s worth noting that the slice operator [:] can be used to create a copy of a sublist within a list as well.

In the below example, you create a new list called copied_list that contains a copy of the elements of mylist starting at index 1 (the second element) and up to but not including index 3 (the fourth element).


# Initialize list
mylist = ["Python","Spark", "Hadoop","Java","C++"]
print("Original list: ", mylist)

# Using the slice operator [:]
copied_list = mylist[1:3]
print("Copied the list: ", copied_list)

Yields below output.

python list copy

You can also create a copy of a list using the slicing syntax. This creates a new list copied_list that contains all of the elements of mylist. Since slicing creates a shallow copy of the list, any modifications made to the nested objects in the new list will also affect the original list.


# Initialize list
mylist = [1, 2, 3, [4, 5]]
copied_list = mylist[:]

# let's modify the nested list 
# In the copied_list
copied_list[3].append(6)

print("Original list: ", mylist)
print("Copied list: ", copied_list)

Yields below output.

5.2 Using the list() Constructor

You can also create a copy of a list using the list() constructor. For example, you first create a mylist with five elements. You then use the list() constructor to create a copy of the list and assign it to a new variable called copied_list.


# Initialize list
mylist = ["Python","Spark", "Hadoop","Java","C++"]
print("Original list: ", mylist)

# Create a copy of the list 
# Using the list() constructor
copied_list = list(mylist)
print("Copied the list: ", copied_list)

# Output:
# Original list:  ['Python', 'Spark', 'Hadoop', 'Java', 'C++']
# Copied the list:  ['Python', 'Spark', 'Hadoop', 'Java', 'C++']

5.3 Using deepcopy() Method

You can also create a deep copy of a list using the deepcopy() method from the copy module. This method creates a new object with a new memory address that is completely independent of the original list. Any modifications made to the copied list will not affect the original list.


import copy

mylist = [1, 2, [3, 4], 5]
copied_list = copy.deepcopy(mylist)

# Copying a list 
# Using deepcopy()
copied_list[2].append(6)

print("Original list: ", mylist)
print("Copied list: ", copied_list)

Yields below output.

python list copy

6. Conclusion

In this article, I have explained the Python list copy() method and using its syntax, parameter, and usage how to copy all the elements from a given list with examples. This method does not take any parameters and returns a new list object.

The copy() performs a shallow copy meaning a new list is created with the same elements as the original list, but the elements themselves are not copied. Instead, the new list contains references to the same objects as the original list. So any changes to the copied list also reflect on the original list.

Happy Learning !!