The simplest way to copy a matrix in Python

To copy a matrix to another variable in Python, you have three main options, and which one you use depends on whether you want the copy to be independent of the original. The most common mistake is writing new_matrix = old_matrix, which does not create a copy at all — it just creates a second name pointing to the same data in memory. When you change one, the other changes too.

The safest approach for most situations is to use the copy.deepcopy() function, which creates a completely independent copy. If you are working with a straightforward list of lists (the most basic matrix structure in Python), you can also use a list comprehension, which is faster and does not require importing anything.

Key Takeaways

  • Assigning a matrix with = creates a reference to the same data, not a copy, so changes to one affect the other.
  • The copy.deepcopy() function creates a fully independent copy that will not change when the original changes.
  • A list comprehension like new_matrix = [row[:] for row in old_matrix] copies a straightforward matrix without importing extra modules.
  • NumPy matrices can be copied with the .copy() method, which is built into the NumPy array object.
  • Shallow copies (created with copy.copy()) work for single-level lists but fail for matrices because they only copy the outer list.

Why assignment alone does not create a copy

When you write matrix_b = matrix_a, Python does not duplicate the data. Instead, it creates a second variable that points to the exact same object in memory. Both variables refer to the same matrix, so if you modify one, you see the change in both.

This happens because lists in Python are mutable — they can be changed after creation. Python optimizes memory by not copying data unless you explicitly ask for it. You can test this yourself: create a matrix, assign it to a new variable, change one element in the new variable, and check the original. You will see the change reflected there too.

Using deepcopy for a completely independent copy

The copy.deepcopy() function creates a new matrix where every element, including nested lists, is copied independently. This is the most reliable method when you need the copy to be completely separate from the original.

Here is how to use it:

  1. At the top of your Python file, write import copy
  2. Create your original matrix: original = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
  3. Copy it with duplicate = copy.deepcopy(original)
  4. Now change the duplicate: duplicate[0][0] = 99
  5. Check the original — it still shows 1 in that position, not 99

The word "deep" matters here. deepcopy() recursively copies everything inside the matrix, including all nested lists. This is slower than other methods for very large matrices, but it is the safest choice when you are unsure what your data contains.

Using list comprehension for straightforward matrices

If your matrix is a straightforward list of lists (the most common structure), a list comprehension is faster than deepcopy and does not require importing anything. It copies each row individually, creating a new independent matrix.

The syntax is: new_matrix = [row[:] for row in old_matrix]

Breaking this down: [row[:] for row in old_matrix] means "for each row in the old matrix, create a new list containing all elements of that row (the [:] slice), and put all these new rows into a new list." The result is a matrix where the outer list is new, and each inner row is also new.

Test it the same way: create a matrix, copy it with a list comprehension, change one element in the copy, and verify the original is unchanged. This method works well for matrices with numbers, strings, or other straightforward data types.

Copying NumPy arrays with the built-in method

If you are using NumPy (a library for numerical computing), matrices are stored as ndarray objects, not plain Python lists. NumPy arrays have their own .copy() method built in, which is the standard way to duplicate them.

The syntax is straightforward: new_array = old_array.copy()

This creates an independent copy of the NumPy array. You do not need to import the copy module — the method is part of the array object itself. NumPy's copy method is optimized for the way NumPy stores data, so it is faster than deepcopy for large numerical matrices.

Why shallow copy does not work for matrices

You might encounter copy.copy(), which creates a shallow copy. For a matrix, this is almost never what you want. A shallow copy duplicates only the outer list, not the inner rows. The inner lists still point to the original data.

If you use shallow = copy.copy(original) and then change shallow[0][0], the original matrix changes too, because both the original and the shallow copy point to the same inner lists. The only time shallow copy works is if your matrix contains immutable data (like tuples instead of lists), but even then, deepcopy or list comprehension is clearer.

Comparing the three main methods

MethodSyntaxSpeedWhen to use
List comprehension[row[:] for row in old_matrix]Faststraightforward lists of lists with basic data types
deepcopycopy.deepcopy(old_matrix)SlowerComplex nested structures or when unsure
NumPy .copy()old_array.copy()FastNumPy arrays and numerical computing

Frequently Asked Questions

What happens if I just use the equals sign to copy a matrix?

The equals sign creates a reference, not a copy. Both variables point to the same matrix in memory. Any change to one is when ready visible in the other. This is rarely what you want when copying a matrix.

Is deepcopy always the safest choice?

Deepcopy is safe but slower, especially for large matrices. For straightforward lists of lists, a list comprehension is faster and equally safe. For NumPy arrays, use the built-in .copy() method instead.

Can I copy just one row of a matrix?

Yes. Use new_row = old_matrix[0][:] to copy the first row, or new_row = old_matrix[0].copy() for a NumPy row. The [:] slice creates a new list with the same elements.

Does the list comprehension method work for matrices with objects inside?

List comprehension copies the outer structure and the inner lists, but if your matrix contains objects (like custom classes), those objects themselves are still referenced, not copied. Use deepcopy if you need the objects inside to be copied too.

What is the difference between copying and cloning a matrix?

In Python, "copying" and "cloning" mean the same thing — creating a new independent matrix. The term "cloning" is more common in other languages, but Python developers typically just say "copy."