Using deepcopy() or copy() is a good solution. For a simple 2D-array case

y = [row[:] for row in x]
Answer from Ryan Ye on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › array-copying-in-python
Array Copying in Python - GeeksforGeeks
April 30, 2025 - Explanation: The deepcopy() method creates a completely independent copy of the 2D array (image). The code rotates the matrix by first reversing each row and then transposing the result.
Discussions

Why is copying a list so damn difficult in python?
This kind of confusion is mostly because you're thinking about python's object model wrong, or don't fully understand it. Perhaps this is a better way to think about things. Say you have this data structure: x = [ [1,2,3], [4,5,6], ] This statement creates 3 lists: 2 inner lists and one outer list. A reference to the outer list is then made available under the name x. When you execute this statement: y = x no data gets copied. You still have the same 3 lists in memory somewhere. All this did is make the outer list availible under the name y, in addition to its previous name x. When you execute this statement: y = list(x) or y = x[:] This creates a new list with the same contents as x. List x contained a reference to the 2 inner lists, so the new list will also contain a reference to those same 2 inner lists. Only one list is copied—the outer list. Now there are 4 lists in memory, the two inner lists, the outer list, and the copy of the outer list. The original outer list is available under the name x, and the new outer list is made available under the name y. The inner lists have not been copied! You can access and edit the inner lists from either x or y at this point! If you have a two dimensional (or higher) list, or any kind of nested data structure, and you want to make a full copy of everything, then you want to use the deepcopy() function in the copy module. Your solution also works for 2-D lists, as iterates over the items in the outer list and makes a copy of each of them, then builds a new outer list for all the inner copies. Hope this explanation helps. More on reddit.com
🌐 r/learnpython
30
9
March 16, 2013
Copying nested lists in Python - Stack Overflow
I'm trying to copy a 2D list, but I'm not sure what to replace with the variable names you provide. 2019-01-10T01:18:54.637Z+00:00 ... @JohnLocke b is the new list, a is the old one. x is used internally. 2019-04-09T01:29:41.427Z+00:00 ... for me, path was a 2D array and I wanted to copy path[i] ... More on stackoverflow.com
🌐 stackoverflow.com
How do i copy out one row of a 2d array into a 1d array? - Processing 2.x and 3.x Forum
Processing is an electronic sketchbook, a language and a worldwide community. This is its forum. More on forum.processing.org
🌐 forum.processing.org
August 12, 2015
What is the fastest way to copy a 2D array in Python? - Stack Overflow
I have to make a very large number of simulations on a R*C grid. These simulations are altering the grid, so I need to copy my reference grid before each, and then apply my simulating function on ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
GitHub
gist.github.com › a050c5b3e6a4866b8949
Deep Copy a 2d array in python · GitHub
Deep Copy a 2d array in python. GitHub Gist: instantly share code, notes, and snippets.
🌐
Aims
python.aims.ac.za › pages › cp_mutable_objs.html
21. Be careful: copying arrays, lists (and more) — AIMS Python 0.7 documentation
However, copy.deepcopy should always be the most reliable option. As noted above, arrays are similar to lists in some ways, and this is one of them. Briefly, a 2D array in programming is often best thought of as an "array of arrays", rather than just a matrix of numbers.
🌐
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.matrix.copy.html
numpy.matrix.copy — NumPy v2.1 Manual
>>> import copy >>> a = np.array([1, 'm', [2, 3, 4]], dtype=object) >>> c = copy.deepcopy(a) >>> c[2][0] = 10 >>> c array([1, 'm', list([10, 3, 4])], dtype=object) >>> a array([1, 'm', list([2, 3, 4])], dtype=object)
🌐
Reddit
reddit.com › r/learnpython › why is copying a list so damn difficult in python?
r/learnpython on Reddit: Why is copying a list so damn difficult in python?
March 16, 2013 -

I am sure there is a reason for this design in Python but why is it so difficult to clone a list? Copying a variable is easy

foo = 1
bar = foo
bar = 2
print bar # returns 2
print foo # returns 1

But trying to do the same thing, which is what makes sense in my mind coming from other languages, does not work. You have to add the list splice otherwise it doesn't work.

Then I spent a day trying to figure out how to copy a 2d list and that was even more frustrating. I had the following code and none of these worked to properly copy a list without modifying the original.

foo = [[1,2], [3,4], [5,6]]
bar = foo # doesnt work
bar = foo[:] # doesnt work
bar = list(foo) # doesnt work
bar = list(foo[:]) # doesnt work
bar = []
bar += foo # doesnt work
bar = []
bar.append(foo) # doesnt work
for i, x in enumerate(foo):
    bar[i] = x[:]
# didnt work

I am sure I tried a few more methods and none of them worked. It was finally this method that was the only way I could find to copy a 2d list, modify the new one without affecting the original list

bar = [x[:] for x in foo]

So why is copying a list in python just so hard?

Top answer
1 of 5
41
This kind of confusion is mostly because you're thinking about python's object model wrong, or don't fully understand it. Perhaps this is a better way to think about things. Say you have this data structure: x = [ [1,2,3], [4,5,6], ] This statement creates 3 lists: 2 inner lists and one outer list. A reference to the outer list is then made available under the name x. When you execute this statement: y = x no data gets copied. You still have the same 3 lists in memory somewhere. All this did is make the outer list availible under the name y, in addition to its previous name x. When you execute this statement: y = list(x) or y = x[:] This creates a new list with the same contents as x. List x contained a reference to the 2 inner lists, so the new list will also contain a reference to those same 2 inner lists. Only one list is copied—the outer list. Now there are 4 lists in memory, the two inner lists, the outer list, and the copy of the outer list. The original outer list is available under the name x, and the new outer list is made available under the name y. The inner lists have not been copied! You can access and edit the inner lists from either x or y at this point! If you have a two dimensional (or higher) list, or any kind of nested data structure, and you want to make a full copy of everything, then you want to use the deepcopy() function in the copy module. Your solution also works for 2-D lists, as iterates over the items in the outer list and makes a copy of each of them, then builds a new outer list for all the inner copies. Hope this explanation helps.
2 of 5
15
squeal gray tie pocket jellyfish sable cheerful cow attraction expansion This post was mass deleted and anonymized with Redact
🌐
Delft Stack
delftstack.com › home › howto › python › copy 2d array in python
How to Copy a 2D Array in Python | Delft Stack
February 2, 2024 - Python offers a range of factory functions that can be used to create a copy of an array or any other mutable object in Python. These mutable objects include dictionaries, sets, and lists. NumPy offers the copy() function.
Find elsewhere
🌐
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.copy.html
numpy.copy — NumPy v2.1 Manual
Note that, np.copy clears previously set WRITEABLE=False flag. >>> a = np.array([1, 2, 3]) >>> a.flags["WRITEABLE"] = False >>> b = np.copy(a) >>> b.flags["WRITEABLE"] True >>> b[0] = 3 >>> b array([3, 2, 3])
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.copy.html
numpy.copy — NumPy v2.4 Manual
Note that, np.copy clears previously set WRITEABLE=False flag. >>> a = np.array([1, 2, 3]) >>> a.flags["WRITEABLE"] = False >>> b = np.copy(a) >>> b.flags["WRITEABLE"] True >>> b[0] = 3 >>> b array([3, 2, 3])
🌐
Medium
medium.com › @alexppppp › replacing-part-of-2d-array-with-another-2d-array-in-numpy-c83144576ddc
Replacing Part of 2D Array with Another 2D Array in Numpy | Medium
December 29, 2021 - and replaced with this array the elements in array a in the 4th & 5th rows and in the 3rd, 4th, 5th & 6th columns. Again, this solution was easy too! A line of code a_copy[3:5,2:6] = a_copy[3:5,2:6] * ~b_mask_boolean + b * b_mask_boolean did the job.
🌐
NumPy
numpy.org › doc › 2.2 › reference › generated › numpy.matrix.copy.html
numpy.matrix.copy — NumPy v2.2 Manual
>>> import copy >>> a = np.array([1, 'm', [2, 3, 4]], dtype=object) >>> c = copy.deepcopy(a) >>> c[2][0] = 10 >>> c array([1, 'm', list([10, 3, 4])], dtype=object) >>> a array([1, 'm', list([2, 3, 4])], dtype=object)
🌐
NumPy
numpy.org › doc › 2.2 › reference › generated › numpy.copy.html
numpy.copy — NumPy v2.2 Manual
Note that, np.copy clears previously set WRITEABLE=False flag. >>> a = np.array([1, 2, 3]) >>> a.flags["WRITEABLE"] = False >>> b = np.copy(a) >>> b.flags["WRITEABLE"] True >>> b[0] = 3 >>> b array([3, 2, 3])
🌐
Processing Forum
forum.processing.org › two › discussion › 12044 › how-do-i-copy-out-one-row-of-a-2d-array-into-a-1d-array.html
How do i copy out one row of a 2d array into a 1d array? - Processing 2.x and 3.x Forum
August 12, 2015 - weird. why would anyone want to set things up that way? a copy should be a copy, not an alias. seems like it would make a lot more trouble than it would save. ... W/o aliasing object references, we couldn't be able to pass them into methods. Since parameters are local variables too that should point to the object reference as well. If we need a shallow clone() of it, we gotta be explicit about it! :-B ... And just for completeness' sake, here's how to fully clone() a 2D array: :ar!
Top answer
1 of 2
16

I used a bash variable for setting up the timeit tests:

setup="""
R = 100
C = 100
from copy import deepcopy
import numpy as np
ref = [[i for i in range(C)] for _ in range(R)]
ref_np = np.array(ref)
cp = [[100 for i in range(C)] for _ in range(R)]
cp_np = np.array(cp)
"""

Just for convenience, I also set a temporary alias pybench:

alias pybench='python3.5 -m timeit -s "$setup" $1'

Python 3

Python 3.5.0+ (default, Oct 11 2015, 09:05:38)

  • Deepcopy:

    >>> pybench "cp = deepcopy(ref)"
    100 loops, best of 3: 8.29 msec per loop
    
  • Modifying pre-created array using index:

    >>> pybench \
    "for y in range(R):
        for x in range(C):
            cp[y][x] = ref[y][x]"
    1000 loops, best of 3: 1.16 msec per loop
    
  • Nested list comprehension:

    >>> pybench "cp = [[x for x in row] for row in ref]"
    1000 loops, best of 3: 390 usec per loop
    
  • Slicing:

    >>> pybench "cp = [row[:] for row in ref]"
    10000 loops, best of 3: 45.8 usec per loop
    
  • NumPy copy:

    >>> pybench "cp_np = np.copy(ref_np)"
    100000 loops, best of 3: 6.03 usec per loop
    
  • Copying to pre-created NumPy array:

    >>> pybench "np.copyto(cp_np, ref_np)"
    100000 loops, best of 3: 4.52 usec per loop
    

There is nothing very surprising in these results, as you might have guessed, use NumPy is enormously faster, especially if one avoids creating a new table each time.

2 of 2
0

To add to the answer from Delgan, numpy copy's documentation says to use numpy.ndarray.copy as the preferred method. So for now, without doing a timing test, I will use numpy.ndarray.copy

https://numpy.org/doc/stable/reference/generated/numpy.copy.html

https://numpy.org/doc/stable/reference/generated/numpy.ndarray.copy.html

🌐
Drbeane
drbeane.github.io › python_dsci › pages › array_2d.html
2-Dimensional Arrays — Python for Data Science
The arrays we have worked with up to this point have all been one-dimensional arrays which consist of a sequence of numbers in a linear order. Numpy provides us with tools for creating and working with higher dimensional arrays. In this lesson, we will work exclusively with 2D arrays, which ...
🌐
Reddit
reddit.com › r/learnpython › copying parts of matrix to new matrix?
r/learnpython on Reddit: Copying parts of matrix to new matrix?
January 20, 2022 -

I have the following matrix:

matrix =     [
        [ 9, 0, 0, 0, 8, 0, 3, 0, 0 ],         
        [ 2, 0, 0, 2, 5, 0, 7, 0, 0 ],         
        [ 0, 2, 0, 3, 0, 0, 0, 0, 4 ],
     ] 

Say that I want to make "new_matrix" have a 3x3 block matrix which is copied from "matrix". In this case, I want new_matrix to have:

new_matrix =     [         
        [8 ,0 ,3 ],         
        [5, 0, 7 ],         
        [0, 0, 0 ],     
    ] 

Essentially, what I want is to tell my program to copy the values from the row index [0:0+2] and from the columns [4:4+2]. I tried using a for-loop to go through the requested indexes and adding those values into new_matrix:

new_matrix = [             
        [0, 0, 0 ],             
        [0, 0, 0 ],             
        [0, 0, 0 ],         
    ]  
for i in range(len(matrix[0:0+2])):         
    for j in range(len(matrix[i][4:4+2])):             
        new_matrix = matrix[i][j] 

This doesn't seem to work. What am I doing wrong?

🌐
YouTube
youtube.com › hey delphi
Array : What is the fastest way to copy a 2D array in Python? - YouTube
Array : What is the fastest way to copy a 2D array in Python?To Access My Live Chat Page, On Google, Search for "hows tech developer connect"As promised, I h...
Published   April 13, 2023
Views   3
🌐
Quora
quora.com › How-do-I-copy-a-2D-array-in-1D-array-in-column-major-fashion-COLUMN-WISE
How to copy a 2D array in 1D array in column major fashion (COLUMN –WISE) - Quora
Answer: #include int main() { int i, j, m, n,k=0; int matrix[10][20]; int arr2[16]; printf("2D-Array\n"); printf("Enter number of rows : "); scanf("%d", &m); printf("Enter number of columns : "); scanf("%d", &n); for (i = 0; i
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.matrix.copy.html
numpy.matrix.copy — NumPy v2.4 Manual
>>> import copy >>> a = np.array([1, 'm', [2, 3, 4]], dtype=object) >>> c = copy.deepcopy(a) >>> c[2][0] = 10 >>> c array([1, 'm', list([10, 3, 4])], dtype=object) >>> a array([1, 'm', list([2, 3, 4])], dtype=object)