← Back to Chapters

Python Copy Lists

? Python Copy Lists

⚡ Quick Overview

In Python, assigning one list to another using = does not create a new copy. It only creates a new reference to the same list in memory. To avoid accidentally changing the original list, you need to create an explicit copy using:

  • copy() method
  • list() constructor
  • Slicing: [:]
  • copy.deepcopy() for nested (multidimensional) lists

? Key Concepts

  • Reference, not copy: b = a points b to the same list as a.
  • Shallow copy: Copies the top-level list but not nested inner lists.
  • Deep copy: Copies the entire structure, including all nested lists.
  • Avoid side effects: Use copying to prevent unexpected changes to the original data.

? Syntax and Theory

When you write:

a = [1, 2, 3]
b = a

Both a and b refer to the same list in memory. Changing one will affect the other.

To avoid this, you use a copying technique:

  • Shallow copy: copy(), list(), and slicing [:] create a new list with the same elements.
  • Deep copy: copy.deepcopy() recursively copies nested lists so that inner lists are independent.

? Code Examples

? Using copy() Method

The simplest and most readable way to copy a list is by using the copy() method.

? View Code Example
fruits = ["apple", "banana", "cherry"]
copy_list = fruits.copy()
print(copy_list)

? Using list() Constructor

You can also pass an existing list to the list() constructor to create a new list.

? View Code Example
fruits = ["apple", "banana", "cherry"]
copy_list = list(fruits)
print(copy_list)

✂️ Using Slicing

Slicing with [:] returns a shallow copy of the list.

? View Code Example
fruits = ["apple", "banana", "cherry"]
copy_list = fruits[:]
print(copy_list)

? Deep Copy for Nested Lists

For nested lists (lists inside lists), a shallow copy is not enough. Use copy.deepcopy() to make a full independent copy.

? View Code Example
import copy

nested = [[1, 2], [3, 4]]
deep_copy = copy.deepcopy(nested)

nested[0][0] = 99

print("Original:", nested)
print("Deep copy:", deep_copy)

? Live Output and Explanation

? What You Will See

For the simple copy examples, the output will be a new list with the same elements:

["apple", "banana", "cherry"]

In the deep copy example, you modify nested after creating deep_copy. The printed result will look like:

Original: [[99, 2], [3, 4]]
Deep copy: [[1, 2], [3, 4]]

This shows that the deep copy is completely independent: changes in nested do not affect deep_copy.

? Tips & Best Practices

  • Use copy() or slicing [:] for simple, non-nested lists.
  • Use deepcopy() when working with nested or multidimensional lists.
  • Remember that = does not copy a list; it only assigns a new reference.
  • Prefer descriptive variable names like original_list and copied_list for clarity.
  • Be careful when passing lists to functions—know whether they are being copied or referenced.

? Try It Yourself

  • Copy a list of numbers using slicing [:] and print both lists.
  • Use the copy() method to duplicate a list of strings, then modify the copy and observe the original.
  • Create a nested list (e.g., [[1, 2], [3, 4]]) and test the difference between a shallow copy and a deep copy.
  • Experiment by modifying elements in the original and copied lists to see when changes are shared or independent.