python
coding
assignment
clone

How do I clone a list so that it doesn't change unexpectedly after assignment?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

In Python, when you assign a list to another variable using =, both variables point to the same list object. This means that changes made to one list will affect the other. To avoid this, you need to clone or copy the list to create a new independent object.

Here are several ways to properly clone a list:


1. Using the copy() Method (Shallow Copy)

The copy() method creates a shallow copy of the list.

Example:

python
1original = [1, 2, 3, [4, 5]]
2cloned = original.copy()
3
4cloned[0] = 99  # Modify the cloned list
5print(original)  # Output: [1, 2, 3, [4, 5]]  (unchanged)
6print(cloned)    # Output: [99, 2, 3, [4, 5]]

Note:
For nested lists or other mutable objects, a shallow copy still references the same inner objects. Changes to the nested objects will affect both lists.


2. Using Slicing ([:])

Slicing creates a shallow copy of the list.

Example:

python
1original = [1, 2, 3, [4, 5]]
2cloned = original[:]
3
4cloned[0] = 99
5print(original)  # Output: [1, 2, 3, [4, 5]] (unchanged)
6print(cloned)    # Output: [99, 2, 3, [4, 5]]

Like copy(), this method works well for shallow copies but won't clone nested objects.


3. Using list() Constructor

The list() constructor also creates a shallow copy of the list.

Example:

python
1original = [1, 2, 3, [4, 5]]
2cloned = list(original)
3
4cloned[0] = 99
5print(original)  # Output: [1, 2, 3, [4, 5]] (unchanged)
6print(cloned)    # Output: [99, 2, 3, [4, 5]]

4. Using copy.deepcopy() (Deep Copy)

To completely clone a list, including nested objects, use the copy.deepcopy() function from the copy module. This creates a deep copy of the list.

Example:

python
1import copy
2
3original = [1, 2, 3, [4, 5]]
4cloned = copy.deepcopy(original)
5
6cloned[3][0] = 99  # Modify the nested list
7print(original)  # Output: [1, 2, 3, [4, 5]] (unchanged)
8print(cloned)    # Output: [1, 2, 3, [99, 5]]
  • Shallow Copy (copy() or slicing): Nested objects are shared.
  • Deep Copy (copy.deepcopy()): Nested objects are fully cloned.

5. Using List Comprehension

If the list is flat (no nested objects), you can use list comprehension to clone it.

Example:

python
1original = [1, 2, 3, 4]
2cloned = [item for item in original]
3
4print(cloned)  # Output: [1, 2, 3, 4]

This creates a new list with the same items.


Comparison of Methods

MethodTypeSupports Nested Objects
list.copy()Shallow Copy❌ No
Slicing ([:])Shallow Copy❌ No
list() ConstructorShallow Copy❌ No
copy.deepcopy()Deep Copy✅ Yes
List ComprehensionShallow Copy❌ No

Summary

  • Use list.copy(), slicing ([:]), or list() for shallow copies.
  • Use copy.deepcopy() for deep copies (to handle nested objects).
  • Avoid simple assignment (=) if you want independent copies of the list. 🚀

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.