list.copy()
list.copy() Returns:
list · Updated March 14, 2026 · List Methods lists methods copying
The .copy() method creates a shallow copy of a list. This is useful when you need to duplicate a list without modifying the original. It’s equivalent to list[:] or list(list).
Syntax
list.copy()
Parameters
None. This method takes no parameters.
Examples
Basic usage
Create a copy of a list:
original = [1, 2, 3, 4, 5]
copied = original.copy()
print(copied)
# [1, 2, 3, 4, 5]
# Modifying the copy does not affect the original
copied.append(6)
print(original)
# [1, 2, 3, 4, 5]
print(copied)
# [1, 2, 3, 4, 5, 6]
Copying a list with nested objects
The key thing to understand is that .copy() creates a shallow copy. The outer list is new, but any nested objects are references to the originals:
original = [[1, 2], [3, 4], [5]]
copied = original.copy()
# The outer lists are different objects
print(original is copied)
# False
# But nested lists are the same objects
print(original[0] is copied[0])
# True
# Modifying a nested list affects both
copied[0].append(99)
print(original[0])
# [1, 2, 99]
print(copied[0])
# [1, 2, 99]
Different ways to copy a list
There are several ways to copy a list in Python:
original = [1, 2, 3]
# Method 1: .copy()
copy1 = original.copy()
# Method 2: Slice notation
copy2 = original[:]
# Method 3: list() constructor
copy3 = list(original)
# Method 4: List comprehension
copy4 = [x for x in original]
print(copy1, copy2, copy3, copy4)
# [1, 2, 3] [1, 2, 3] [1, 2, 3] [1, 2, 3]
All four methods create a shallow copy.
Deep copying nested structures
For nested lists where you want completely independent copies, use copy.deepcopy():
import copy
original = [[1, 2], [3, 4]]
deep_copied = copy.deepcopy(original)
# Now nested lists are completely independent
deep_copied[0].append(99)
print(original[0])
# [1, 2]
print(deep_copied[0])
# [1, 2, 99]
Common Patterns
Avoiding mutation when passing lists to functions
A common use case is passing a list to a function without letting the function modify the original:
def process_list(items):
# Work on a copy so original is preserved
working = items.copy()
working.sort()
return working
numbers = [3, 1, 4, 1, 5]
result = process_list(numbers)
print(numbers) # Unchanged: [3, 1, 4, 1, 5]
print(result) # [1, 1, 3, 4, 5]
Creating a list to modify in a loop
When you need to modify a list during iteration, working on a copy is often safer:
items = [1, 2, 3, 4, 5]
for item in items.copy():
if item % 2 == 0:
items.remove(item)
print(items)
# [1, 3, 5]
Performance Note
.copy()is O(n) where n is the number of elements- For shallow copies,
.copy()is slightly faster than slice notation[:] - For large lists, this can be significant
import timeit
large_list = list(range(100000))
# Test .copy() vs [:]
t1 = timeit.timeit(lambda: large_list.copy(), number=1000)
t2 = timeit.timeit(lambda: large_list[:], number=1000)
print(f".copy(): {t1:.4f}s")
print(f"[:]: {t2:.4f}s")
# .copy() is typically faster
Shallow vs Deep Copy Summary
| Method | Creates new outer list | Copies nested objects |
|---|---|---|
.copy() | Yes | No (references) |
list[:] | Yes | No (references) |
list() | Yes | No (references) |
copy.deepcopy() | Yes | Yes (recursively) |
See Also
- list::list.append() — add elements to a list
- list::list — the list constructor and type overview
- copy module::copy.deepcopy — create deep copies of nested objects
- built-in::len — get the length of a list