Home »
Python
Python - Copy Lists
Last Updated : May 01, 2025
In Python, copying lists can be done using methods like copy()
or slicing. These techniques create a duplicate of the original list. In this chapter, we will learn various approaches to copy lists with the help of examples.
Copying a List Using the copy() Method
The copy()
method creates a shallow copy of the list, meaning it creates a new list with the same elements but does not copy nested objects.
Example
In this example, we are copying the list of cities using the copy()
method.
# Creating a list of cities
cities = ["New York", "London", "Paris", "Berlin"]
# Copying the list
cities_copy = cities.copy()
# Displaying the copied list
print("Original list:", cities)
print("Copied list:", cities_copy)
The output of the above code would be:
Original list: ['New York', 'London', 'Paris', 'Berlin']
Copied list: ['New York', 'London', 'Paris', 'Berlin']
Copying a List Using the list() Constructor
You can also create a copy of a list using the list()
constructor. This works in the same way as the copy()
method by returning a new list with the same elements.
Example
In this example, we are copying the list of cities using the list()
constructor.
# Creating a list of cities
cities = ["New York", "London", "Paris", "Berlin"]
# Copying the list using list() constructor
cities_copy = list(cities)
# Displaying the copied list
print("Original list:", cities)
print("Copied list:", cities_copy)
The output of the above code would be:
Original list: ['New York', 'London', 'Paris', 'Berlin']
Copied list: ['New York', 'London', 'Paris', 'Berlin']
Copying a List Using Slicing
Slicing is another technique to create a shallow copy of a list. The syntax list[start:end]
can be used to create a new list that contains the same elements as the original list.
Example
In this example, we are copying the list of cities using slicing.
# Creating a list of cities
cities = ["New York", "London", "Paris", "Berlin"]
# Copying the list using slicing
cities_copy = cities[:]
# Displaying the copied list
print("Original list:", cities)
print("Copied list:", cities_copy)
The output of the above code would be:
Original list: ['New York', 'London', 'Paris', 'Berlin']
Copied list: ['New York', 'London', 'Paris', 'Berlin']
Copying a List Using the list Constructor with Iteration
You can create a copy of a list by iterating through the original list and adding each element to a new list. This is an alternative approach that gives more control over the copying process.
Example
In this example, we are copying the list of cities using a for loop and the list()
constructor.
# Creating a list of cities
cities = ["New York", "London", "Paris", "Berlin"]
# Copying the list using iteration and list constructor
cities_copy = list(city for city in cities)
# Displaying the copied list
print("Original list:", cities)
print("Copied list:", cities_copy)
The output of the above code would be:
Original list: ['New York', 'London', 'Paris', 'Berlin']
Copied list: ['New York', 'London', 'Paris', 'Berlin']
Deep Copying a List Using copy Module
In some cases, you might want to create a deep copy of a list, especially if the list contains nested lists or objects. You can use the copy()
method from the copy
module to create a deep copy of the list.
Example
In this example, we are copying a list that contains another list (nested list) using the deepcopy()
method from the copy
module.
import copy
# Creating a list of cities with a nested list
cities = ["New York", "London", ["Paris", "Berlin"], "Tokyo"]
# Deep copying the list
cities_copy = copy.deepcopy(cities)
# Displaying the copied list
print("Original list:", cities)
print("Copied list:", cities_copy)
The output of the above code would be:
Original list: ['New York', 'London', ['Paris', 'Berlin'], 'Tokyo']
Copied list: ['New York', 'London', ['Paris', 'Berlin'], 'Tokyo']
Exercise
Select the correct option to complete each statement about copying lists in Python.
- Which method can be used to create a shallow copy of a list in Python?
- What will
list2 = list1
do in Python if list1 = [1, 2, 3]
?
- Which method would you use to make a deep copy of a list?
Advertisement
Advertisement