Copy a List in Python
In Python, you can copy a list using different methods such as the copy()
method, slicing, list comprehension, and the list()
constructor. Using these methods ensures that changes to the new list do not affect the original list.
Examples
1. Copy a List Using the copy()
Method
The copy()
method creates a shallow copy of the original list.
# Creating the original list
original_list = [1, 2, 3, 4, 5]
# Copying the list using copy() method
copied_list = original_list.copy()
# Modifying the copied list
copied_list.append(6)
# Printing both lists
print("Original List:", original_list)
print("Copied List:", copied_list)
Here, we first create a list named original_list
containing five elements. We then use the copy()
method to create copied_list
, which is a separate list with the same elements as the original.
After copying, we modify the copied_list
by appending 6
. Since copy()
creates a shallow copy, changes to copied_list
do not affect original_list
.
Output:
Original List: [1, 2, 3, 4, 5]
Copied List: [1, 2, 3, 4, 5, 6]
2. Copy a List Using Slicing
We can also copy a list using slicing [:]
, which creates a new list with the same elements.
# Creating the original list
original_list = ["a", "b", "c"]
# Copying using slicing
copied_list = original_list[:]
# Modifying copied list
copied_list.append("d")
# Printing both lists
print("Original List:", original_list)
print("Copied List:", copied_list)
Here, original_list
is a list of three elements. We use slicing [:]
to copy all elements into copied_list
.
After modifying copied_list
by appending “d”, the original_list
remains unchanged, proving that slicing creates a separate copy.
Output:
Original List: ['a', 'b', 'c']
Copied List: ['a', 'b', 'c', 'd']
3. Copy a List Using the list()
Constructor
The list()
constructor can be used to copy an existing list.
# Creating the original list
original_list = [10, 20, 30]
# Copying the list using list() constructor
copied_list = list(original_list)
# Modifying copied list
copied_list.append(40)
# Printing both lists
print("Original List:", original_list)
print("Copied List:", copied_list)
The list()
function takes original_list
as input and creates a new list copied_list
with the same elements.
Appending 40
to copied_list
does not affect original_list
, confirming that they are two separate lists.
Output:
Original List: [10, 20, 30]
Copied List: [10, 20, 30, 40]
4. Copy a List Using List Comprehension
List comprehension can also be used to create a copy of a list.
# Creating the original list
original_list = ["x", "y", "z"]
# Copying the list using list comprehension
copied_list = [item for item in original_list]
# Modifying copied list
copied_list.append("w")
# Printing both lists
print("Original List:", original_list)
print("Copied List:", copied_list)
List comprehension iterates over original_list
and creates a new list copied_list
with identical elements.
After adding “w” to copied_list
, original_list
remains unchanged.
Output:
Original List: ['x', 'y', 'z']
Copied List: ['x', 'y', 'z', 'w']
Conclusion
copy()
method: Best for simple shallow copies.- Slicing
[:]
: Quick and efficient. list()
constructor: Explicit and readable.- List comprehension: Useful for applying modifications while copying.
Each method ensures that modifications to the copied list do not affect the original. Choose based on readability and performance needs.