📋 How to Copy a List in Python – Right Way vs Wrong Way! | TechTown.in
Copying lists in Python might seem simple — just assign it, right? Not quite! If you’re not careful, you might end up with two variables pointing to the same list instead of a copy.
In this guide, you’ll learn the correct ways to copy a list in Python, including copy(), slicing, list(), and what not to do.
❗ The Wrong Way – Simple Assignment (=)
Many beginners assume this will copy the list:
list1 = ["apple", "banana", "cherry"]
list2 = list1
But this only creates a reference, not a new list. Both list1 and list2 now point to the same memory.
Example:
list2[0] = "mango"
print(list1) # ['mango', 'banana', 'cherry']
✅ They both changed — because they’re the same object.
✅ 1. Use copy() Method
This is the recommended and cleanest way to copy a list.
list1 = ["apple", "banana", "cherry"]
list2 = list1.copy()
list2[0] = "mango"
print(list1) # ['apple', 'banana', 'cherry']
print(list2) # ['mango', 'banana', 'cherry']
✅ 2. Use list() Constructor
Another safe way to copy:
list1 = ["a", "b", "c"]
list2 = list(list1)
✅ list2 is now a new list object.
✅ 3. Use Slicing ([:])
You can also copy a list using slicing:
original = [1, 2, 3]
copy_list = original[:]
This creates a shallow copy of the list.
🔄 Summary – How to Copy Lists in Python
| Method | Description | Creates New List? |
|---|---|---|
list2 = list1 | Reference (⚠️ not a real copy) | ❌ No |
list1.copy() | Shallow copy (Pythonic way) | ✅ Yes |
list(list1) | Constructor-based copy | ✅ Yes |
list1[:] | Slice-based copy | ✅ Yes |
🧠 What is a Shallow Copy?
All these methods create a shallow copy, meaning:
- The outer list is copied, but
- If the list contains nested lists, only the references are copied.
Example:
a = [[1, 2], [3, 4]]
b = a.copy()
b[0][0] = 99
print(a) # [[99, 2], [3, 4]]
📌 To fully copy nested lists, use copy.deepcopy() (from the copy module).
🏁 Final Thoughts
Copying a list properly ensures you avoid unintended side effects in your programs. If you’re working with simple (non-nested) lists, copy(), list(), or slicing ([:]) are your best friends.
📘 Learn more Python list tips and tricks at TechTown.in

