How to Copy a List in Python
What you’ll build or solve
You’ll copy a Python list without accidentally changing the original.
When this approach works best
Copying a list works well when you:
Learn Python on Mimo
- Modify a list, but still need the original later, like sorting a copy for display.
- Pass a list into code that mutates it, while keeping your source data unchanged.
- Snapshot a list at a point in time, like saving a “before” version for undo or comparison.
Skip this approach if you only need to read from the list. You can keep one list and avoid extra memory use.
Prerequisites
- Python 3 installed
- You know what a list is
Step-by-step instructions
1) Make a shallow copy
A shallow copy creates a new list, but it still shares any nested objects inside the list. Choose the syntax that reads best for your code.
Option A (common): slicing
items= [1,2,3]
copy_a=items[:]
items.append(4)
print(items)# [1, 2, 3, 4]
print(copy_a)# [1, 2, 3]
Option B: list() constructor
items= [1,2,3]
copy_b=list(items)
items.append(4)
print(copy_b)# [1, 2, 3]
Option C (most readable): .copy() method
items= [1,2,3]
copy_c=items.copy()
items.append(4)
print(copy_c)# [1, 2, 3]
What to look for: All three create a new list that won’t change when you modify items.
2) Deep copy when the list contains nested objects
If your list contains lists or dictionaries that you plan to mutate, a shallow copy is not enough. Use copy.deepcopy() to duplicate nested objects too.
LUA
importcopy
matrix= [[1,2], [3,4]]
deep=copy.deepcopy(matrix)
matrix[0].append(99)
print(matrix)# [[1, 2, 99], [3, 4]]
print(deep)# [[1, 2], [3, 4]]
What to look for: If you modify a nested list inside the original, only a deep copy stays unchanged.
Examples you can copy
Example 1: Sort a copy without changing the original
scores= [30,10,25]
sorted_scores=sorted(scores)
print(scores)
print(sorted_scores)
Example 2: Create a “before” snapshot, then modify the list
tasks= ["email","review","deploy"]
before=tasks.copy()
tasks.remove("review")
print(before)
print(tasks)
Example 3: Copy only part of a list
items= ["a","b","c","d","e"]
first_three=items[:3]
print(first_three)
Example 4: Copy a list of dictionaries safely
If you plan to mutate dictionaries inside the list, deep copy avoids shared references.
importcopy
users= [{"name":"Amina"}, {"name":"Luka"}]
users_copy=copy.deepcopy(users)
users[0]["name"]="Noor"
print(users)
print(users_copy)
Example 5: Copy a list inside a function to avoid side effects
defadd_item(items,new_item):
items=items.copy()
items.append(new_item)
returnitems
original= ["a","b"]
updated=add_item(original,"c")
print(original)
print(updated)
Common mistakes and how to fix them
Mistake 1: Assigning instead of copying
What you might do:
items= [1,2,3]
copy_items=items
items.append(4)
print(copy_items)
Why it breaks: Both variables point to the same list, so changes appear in both.
Correct approach:
items= [1,2,3]
copy_items=items.copy()
items.append(4)
print(copy_items)
Mistake 2: Shallow copying a nested list and expecting it to be independent
What you might do:
matrix= [[1,2], [3,4]]
copy_matrix=matrix.copy()
matrix[0].append(99)
print(copy_matrix)
Why it breaks: The inner lists are still shared, so nested changes show up in the copy.
Correct approach:
importcopy
matrix= [[1,2], [3,4]]
copy_matrix=copy.deepcopy(matrix)
matrix[0].append(99)
print(copy_matrix)
Mistake 3: Using .sort() and expecting a new list
What you might do:
scores= [30,10,25]
sorted_copy=scores.sort()
print(sorted_copy)
Why it breaks: .sort() modifies the list in place and returns None.
Correct approach:
scores= [30,10,25]
sorted_copy=sorted(scores)
print(sorted_copy)
print(scores)
If you want to sort the original list in place:
scores= [30,10,25]
scores.sort()
print(scores)
Troubleshooting
If your “copy” changes when you mutate the original, you probably assigned (b = a) instead of copying, or you made a shallow copy of nested objects.
If only nested items change in both lists, you need a deep copy, use copy.deepcopy(...).
If you get NameError: name 'copy' is not defined, you forgot import copy.
If you see None after sorting, you used .sort() and assigned its return value. Use sorted() for a new list.
Quick recap
- Use
items[:],list(items), oritems.copy()for a shallow copy. - Shallow copies share nested objects, so nested mutations can leak across lists.
- Use
copy.deepcopy()for lists of lists or lists of dicts you plan to mutate. - Avoid
b = awhen you want a copy, it’s the same list. - Use
sorted()when you want a sorted copy,.sort()sorts in place.
Join 35M+ people learning for free on Mimo
4.8 out of 5 across 1M+ reviews
Check us out on Apple AppStore, Google Play Store, and Trustpilot