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.
🌐
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.
🌐
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
🌐
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 › 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?

Find elsewhere
🌐
W3Schools
w3schools.com › python › python_lists_copy.asp
Python - Copy Lists
You can also make a copy of a list by using the : (slice) operator. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com · If you want to report an error, or if you want to make a suggestion, send us an e-mail: help@w3schools.com · HTML Tutorial CSS Tutorial JavaScript Tutorial How To Tutorial SQL Tutorial Python Tutorial W3.CSS Tutorial Bootstrap Tutorial PHP Tutorial Java Tutorial C++ Tutorial jQuery Tutorial
🌐
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)
🌐
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.
🌐
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
🌐
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.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])
🌐
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])
🌐
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!
🌐
CodeSignal
codesignal.com › learn › courses › multidimensional-arrays-and-their-traversal-in-python › lessons › exploring-the-dimensions-a-beginners-guide-to-multidimensional-arrays-in-python
A Beginner's Guide to Multidimensional Arrays in Python
In this example, array is a 2-dimensional array, just like a 3-storey 'apartment building,' where every floor is an inner list. ... All indices in Python arrays are 0-based. Let's say you want to visit an apartment on the second floor (index 1) and bring a package to the first unit (index 0) in this building.
🌐
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.
🌐
Quora
quora.com › What-is-the-difference-between-a-shallow-and-deep-copy-of-an-array-in-Python
What is the difference between a shallow and deep copy of an array in Python? - Quora
Answer: Shallow Copy A shallow copy is a copy of an object that stores the reference of the original elements. It creates the new collection object and then occupying it with reference to the child objects found in the original. It makes copies of the nested objects' reference and doesn't creat...
🌐
Team Treehouse
teamtreehouse.com › community › i-need-to-copy-the-elements-of-array-that-can-not-be-divided-through-4-to-array2
I need to copy the elements of "array" that can NOT be divided through 4, to "array2". (Example) | Treehouse Community
November 7, 2018 - if you look for generic solution you need to loop two times through the array first loop to determine count of elements you need to copy and second this to do the actual copy so your code look like this