|
| Home |
|---|
The Python list copy() method is used to create a new copy of an existing Python list. The copied list contains the same elements as the original list, but it is a separate list object.
The copy() method is useful when you want to modify one list
without changing the original list. This is especially important when
working with datasets, algorithms, loops, functions, and programs where
the original data needs to be preserved.
A common beginner mistake is to use the assignment operator
= when they actually need to copy a list. Assignment does not
create a new list. Instead, both variables refer to the same list object.
Using copy() creates a separate list.
In this tutorial, you will learn how to use the Python
list.copy() method, understand its syntax and return value,
copy lists safely, compare copy() with assignment and slicing,
and understand the difference between shallow copy and deep copy.
The copy() method creates a shallow copy of
a Python list. The new list contains the same elements as the original
list, but the two lists are separate list objects.
For example:
numbers = [10, 20, 30]
new_numbers = numbers.copy()
After this operation, both lists contain the same values:
numbers
[10, 20, 30]
new_numbers
[10, 20, 30]
However, they are separate list objects. Adding or removing an element from one list does not change the other list when the list contains ordinary immutable elements such as integers or strings.
The following example creates a copy of a list using the
copy() method.
numbers = [10, 20, 30]
new_list = numbers.copy()
print(new_list)
[10, 20, 30]
The new list contains the same elements as the original list.
One of the main reasons to use copy() is to create a list that
can be modified independently of the original list.
numbers = [1, 2, 3]
copy_numbers = numbers.copy()
copy_numbers.append(4)
print("Original:", numbers)
print("Copy:", copy_numbers)
Original: [1, 2, 3]
Copy: [1, 2, 3, 4]
The original list remains unchanged because copy_numbers is a
separate list object.
You can also change an element in the copied list without changing the corresponding element in the original list.
numbers = [10, 20, 30]
copy_numbers = numbers.copy()
copy_numbers[0] = 100
print("Original:", numbers)
print("Copy:", copy_numbers)
Original: [10, 20, 30]
Copy: [100, 20, 30]
The assignment to copy_numbers[0] affects only the copied
list.
The copy() method works with lists containing strings as well.
fruits = ["apple", "banana", "cherry"]
new_fruits = fruits.copy()
print(new_fruits)
['apple', 'banana', 'cherry']
The new list contains the same string elements as the original list.
You can copy a list after creating it dynamically during program execution.
numbers = []
for i in range(5):
numbers.append(i)
copy_numbers = numbers.copy()
print(copy_numbers)
[0, 1, 2, 3, 4]
The copy() method works the same way regardless of whether
the list was created directly or built during program execution.
The is operator can be used to check whether two variables
refer to the same object.
numbers = [1, 2, 3]
copy_numbers = numbers.copy()
print(numbers is copy_numbers)
print(numbers == copy_numbers)
False
True
The first result is False because the two variables refer to
different list objects.
The second result is True because the lists contain the same
values.
One of the most important concepts to understand is the difference between copying a list and assigning a list to another variable.
Consider:
numbers = [1, 2, 3]
copy_numbers = numbers
This does not create a copy. Both variables refer to the same list object.
numbers = [1, 2, 3]
copy_numbers = numbers
copy_numbers.append(4)
print("Original:", numbers)
print("Other:", copy_numbers)
Original: [1, 2, 3, 4]
Other: [1, 2, 3, 4]
Changing copy_numbers also changes numbers because
both variables refer to the same list.
| Operation | New List Object? | Original Affected by List Changes? |
|---|---|---|
copy_numbers = numbers |
No | Yes |
copy_numbers = numbers.copy() |
Yes | No |
This distinction is particularly important when working with lists inside functions or when creating backup versions of data.
Use copy() when you need two independent list objects that
initially contain the same elements.
For example, suppose a program has a list of scores:
scores = [75, 82, 91, 68]
If you want to experiment with the data while preserving the original scores, create a copy:
new_scores = scores.copy()
You can then modify new_scores without changing
scores.
The copy() method is not the only way to create a shallow copy
of a Python list. Python provides several alternatives.
copy_list = numbers.copy()
This is an explicit and readable way to copy a list.
numbers = [10, 20, 30]
copy_list = numbers[:]
print(copy_list)
[10, 20, 30]
The expression numbers[:] creates a shallow copy of the list.
numbers = [1, 2, 3]
copy_list = list(numbers)
print(copy_list)
[1, 2, 3]
The list() constructor can create a new list containing the
elements of the original list.
| Method | Creates New List? | Type of Copy |
|---|---|---|
numbers.copy() |
Yes | Shallow copy |
numbers[:] |
Yes | Shallow copy |
list(numbers) |
Yes | Shallow copy |
copy_numbers = numbers |
No | Reference assignment |
The Python copy() method creates a
shallow copy. This means that the outer list is copied,
but objects contained inside the list are not recursively copied.
This distinction becomes important when a list contains mutable objects, such as other lists.
Consider a list containing two inner lists:
data = [[1, 2], [3, 4]]
copy_data = data.copy()
copy_data[0][0] = 99
print(data)
print(copy_data)
[[99, 2], [3, 4]]
[[99, 2], [3, 4]]
At first this may seem surprising. We copied the outer list, so why did changing the copied list also affect the original?
The reason is that copy() creates a shallow copy. The outer
list is new, but the inner lists are still shared between the two lists.
Suppose you have:
data = [[1, 2], [3, 4]]
When you create:
copy_data = data.copy()
Python creates a new outer list, but the inner list objects are still referenced by both lists.
Conceptually:
data → [ inner_list_1, inner_list_2 ]
copy_data → [ inner_list_1, inner_list_2 ]
The outer lists are different, but the nested objects are shared.
A shallow copy copies the outer list but does not recursively copy nested objects.
A deep copy recursively copies the objects contained inside the list as well.
| Feature | Shallow Copy | Deep Copy |
|---|---|---|
| Outer list copied? | Yes | Yes |
| Nested mutable objects copied? | No | Yes |
| Nested lists shared? | Yes | No |
| Python list copy()? | Yes | No |
When a list contains nested mutable objects and you need a completely
independent copy, Python's copy module provides
deepcopy().
import copy
data = [[1, 2], [3, 4]]
copy_data = copy.deepcopy(data)
copy_data[0][0] = 99
print(data)
print(copy_data)
[[1, 2], [3, 4]]
[[99, 2], [3, 4]]
In this example, changing the nested list inside copy_data
does not change the nested list inside data.
The copy() method is useful when you need an independent
version of a list containing values that do not require recursive copying.
Common uses include:
The copy() method is especially useful when you want to sort a
list while preserving the original order.
numbers = [30, 10, 40, 20]
sorted_numbers = numbers.copy()
sorted_numbers.sort()
print("Original:", numbers)
print("Sorted:", sorted_numbers)
Original: [30, 10, 40, 20]
Sorted: [10, 20, 30, 40]
The original list remains in its original order because
sort() was applied to the copied list.
You can also use copy() when you want to reverse a list while
preserving its original order.
numbers = [1, 2, 3, 4]
reversed_numbers = numbers.copy()
reversed_numbers.reverse()
print("Original:", numbers)
print("Reversed:", reversed_numbers)
Original: [1, 2, 3, 4]
Reversed: [4, 3, 2, 1]
A copy can also be useful when you need to remove elements from one version of a list while preserving the original.
numbers = [10, 20, 30, 40]
new_numbers = numbers.copy()
new_numbers.remove(20)
print("Original:", numbers)
print("Modified copy:", new_numbers)
Original: [10, 20, 30, 40]
Modified copy: [10, 30, 40]
Understanding list copying is also important when working with functions. Python lists are mutable objects, so modifying a list inside a function can affect the original list.
Creating a copy allows a function to work with an independent list when that is the desired behavior.
def add_number(numbers):
new_numbers = numbers.copy()
new_numbers.append(100)
return new_numbers
numbers = [1, 2, 3]
result = add_number(numbers)
print("Original:", numbers)
print("Result:", result)
Original: [1, 2, 3]
Result: [1, 2, 3, 100]
Here, the function creates a copy before adding the new value, so the original list remains unchanged.
The need for copy() is closely related to the fact that Python
lists are mutable. A mutable object can be changed after
it has been created.
For example:
numbers = [1, 2, 3]
numbers.append(4)
The existing list has been modified rather than a new list being created. When multiple variables refer to the same list, this can sometimes produce unexpected results.
Using copy() gives you a separate outer list that can be
modified independently.
The copy() method can also be used with an empty list.
numbers = []
new_numbers = numbers.copy()
print(new_numbers)
[]
The result is another empty list object.
Duplicate elements are copied normally.
numbers = [1, 2, 2, 3, 2]
new_numbers = numbers.copy()
print(new_numbers)
[1, 2, 2, 3, 2]
The copy() method does not remove duplicates or change the
values of the original list.
For a shallow copy, the new outer list contains references to the same objects contained in the original list.
For simple immutable values such as integers and strings, this usually behaves exactly as beginners expect because those values cannot be changed in place.
For mutable objects such as nested lists, dictionaries, or other mutable objects, the distinction becomes important because the objects may still be shared.
This:
new_list = original_list
does not create a copy. Both variables refer to the same list.
Use:
new_list = original_list.copy()
when you need a separate list object.
The copy() method creates a shallow copy. Nested mutable
objects are not recursively copied.
For a completely independent nested structure, consider
copy.deepcopy().
The == operator checks whether two lists contain equal values,
while is checks whether they are the same object.
numbers = [1, 2, 3]
new_numbers = numbers.copy()
print(numbers == new_numbers)
print(numbers is new_numbers)
True
False
For a normal list containing immutable values, modifying the copied list does not modify the original list.
However, with nested mutable objects, a shallow copy can still share those nested objects.
| Feature | copy() | deepcopy() |
|---|---|---|
| Creates new outer list? | Yes | Yes |
| Copies nested mutable objects? | No | Yes |
| Type | Shallow copy | Deep copy |
| Module required? | No | copy module |
| Code | New List? | Lists Independent? |
|---|---|---|
b = a |
No | No |
b = a.copy() |
Yes | Yes* |
*The copied outer list is independent, but nested mutable objects may still
be shared because copy() creates a shallow copy.
Creating a shallow copy of a list requires Python to process the elements
of the list, so the time complexity of list.copy() is
O(n), where n is the number of elements in
the list.
The amount of memory required for the new outer list is also proportional to the number of elements.
copy() is a built-in method of Python lists.list.copy().= does not create a copy.list(original_list) can also create a shallow copy.copy().copy.deepcopy() when a deep copy is required.
The copy() method creates a shallow copy of a Python list.
The new list contains the same elements as the original list but is a
separate list object.
Use the copy() method:
numbers = [1, 2, 3]
new_numbers = numbers.copy()
Yes. The copy() method creates a new outer list containing
the elements of the original list.
No. Calling copy() does not modify the original list.
Assignment using = makes another variable refer to the same
list. The copy() method creates a separate list object.
Yes. list.copy() creates a shallow copy. The outer list is
copied, but nested mutable objects are not recursively copied.
If you only need a shallow copy, you can use:
new_list = original_list.copy()
If you need to recursively copy nested objects, use
copy.deepcopy().
copy() creates a shallow copy, while
deepcopy() recursively copies nested objects as well.
Both list.copy() and list[:] create shallow copies
of a list. The copy() method is often more explicit and easier
to understand when the intention is to copy the list.
For a list, both original.copy() and
list(original) create a new shallow list containing the same
elements.
When the list contains ordinary immutable values, changing the copied list does not change the original list because the outer lists are separate objects.
Yes. However, copy() creates a shallow copy, so the nested
list itself is still shared between the original and copied outer lists.
Because copy() creates a shallow copy. The outer list is new,
but the nested mutable list is still referenced by both lists.
The time complexity of copying a list is O(n), where
n is the number of elements in the list.
Struggling with Python syntax, assignments or debugging errors? Learning alone can be frustrating.
Get 1-on-1 Python tutoring and learn faster with clear explanations, real examples and live help.
Home Tutorial Online Live Trainings Contact Us © 2026 All rights reserved by www.PythonTutor.net