Because python passes lists by reference

This means that when you write "b=a" you're saying that a and b are the same object, and that when you change b you change also a, and viceversa

A way to copy a list by value:

new_list = old_list[:]

If the list contains objects and you want to copy them as well, use generic copy.deepcopy():

import copy
new_list = copy.deepcopy(old_list)
Answer from Nikaido on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › copy-list-python-without-reference
Copy List Python Without Reference - GeeksforGeeks
July 23, 2025 - In this approach, we make a copy of the list without reference. This process is also called cloning. This technique takes about 0.039 seconds and is the fastest technique. In this example, we are using list-slicing technique to clone or copy a list. ... # Python program Copy List Without Reference # using Slice Operator original_list = [1, 2, 3, 4, 5] copied_list = original_list[:] # Driver Code print("Original List:", original_list) print("Copied List:", copied_list)
Discussions

python - Create a copy and not a reference of a NumPy array - Stack Overflow
I'm currently working on a Python program that involves using NumPy for image processing. However, I've encountered an issue when trying to create a deep copy of a NumPy array instead of just copying the reference. More on stackoverflow.com
🌐 stackoverflow.com
python list by value not by reference - Stack Overflow
Copya=['help', 'copyright', 'credits', ... a ['help', 'copyright', 'credits', 'license', 'XYZ'] I wanted to append value in list 'b' but the value of list 'a' have also changed. I think I have little idea why its like this (python passes lists by reference).... More on stackoverflow.com
🌐 stackoverflow.com
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
Copy an Array value - not reference
Iterate over the original array in a traditional for loop and insert the elements one by one in your new array. More on reddit.com
🌐 r/learnjava
11
1
March 1, 2022
🌐
Python.org
discuss.python.org › python help
How may I copy a list of list without reference? - Python Help - Discussions on Python.org
October 20, 2023 - I can copy a list without reference a = [3,4,5] b = a.copy() b[0] = -1 print(a) print(b) >>[3, 4, 5] >>[-1, 4, 5] but why can’t I do the same for a list of list? What should I do? a = [[1,1],[1,1]] b = a.copy() b[0][0] = -1 print(a) print(b) >>[[-1, 1], [1, 1]] >>[[-1, 1], [1, 1]]
🌐
Linux Hint
linuxhint.com › copy-array-python
Copy Array in Python – Linux Hint
Create a Python file with the following script to check the use of the assignment operator for copying an array. #Import the NumPy module import numpy as np #Create an array array1 = np.array([34.5, 89.34, 23.6, 45.72, 62.87]) #Copy the array using '=' operator array2 = array1 #Modify the 4th element of the main array array1[3] = 10.12 print("The output after modifying the main array") #Display both arrays print("The content of the main array", array1) print("The content of the copied array", array2) #Modify the 1st element of the copied array array2[0] = 20.34 print("\nThe output after modifying the copied array") #Display both arrays again print("The content of the main array", array1) print("The content of the copied array", array2)
🌐
Wsldp
elearning.wsldp.com › python3 › how-to-copy-list-without-reference-in-python-3
How to Copy List Without Reference in Python 3 - e Learning
July 25, 2021 - To copy an existing list to another one without reference, you can use the copy() method.
Find elsewhere
🌐
Toppr
toppr.com › guides › python-guide › references › methods-and-functions › python-list-copy
Python Copy List: How to copy a list in python, Examples
July 1, 2024 - In python, users can copy a list without references by using the copy(). The copy() generates a shallow list that does not include the references.
🌐
Programiz
programiz.com › python-programming › methods › list › copy
Python list copy (with Code Visualization)
As mentioned above, using = without copy() makes a variable reference the same list without creating a copy.
🌐
Moonbooks
moonbooks.org › Articles › How-to-copy-an-array-matrix-in-python-
How to copy an array (matrix) in python ?
March 30, 2019 - >>> x = np.array([{'a':[1,2,3]}]) >>> y = np.copy(x) >>> y array([{'a': [1, 2, 3]}], dtype=object) >>> y[0]['a'].append(4) >>> y array([{'a': [1, 2, 3, 4]}], dtype=object) >>> x array([{'a': [1, 2, 3, 4]}], dtype=object)
🌐
GeeksforGeeks
geeksforgeeks.org › array-copying-in-python
Array Copying in Python - GeeksforGeeks
April 30, 2025 - In Python, there are several ways to copy arrays, each with different behaviors. The three main methods for copying arrays are: Simply using the assignment operator. ... We can create a copy of an array by using the assignment operator (=). ...
🌐
Aims
python.aims.ac.za › pages › cp_mutable_objs.html
21. Be careful: copying arrays, lists (and more) — AIMS Python 0.7 documentation
import copy A = <some array, list or other mutable object> B = copy.deepcopy( A ) In our own thought-processing and algorithm generation, we might picture the variable A as representing the whole array/list/etc.---that is fine. But when it comes to implementing the program, we need to be aware that it is something different to Python (it is just the starting point reference of the object), so that we can deal with it appropriately.
🌐
NumPy
numpy.org › doc › stable › user › basics.copies.html
Copies and views — NumPy v2.5 Manual
When a new array is created by duplicating the data buffer as well as the metadata, it is called a copy. Changes made to the copy do not reflect on the original array. Making a copy is slower and memory-consuming but sometimes necessary. A copy can be forced by using ndarray.copy.
🌐
Afternerd
afternerd.com › blog › python-copy-list
Python: How to Copy a List? (The Idiomatic Way) - Afternerd
January 13, 2019 - In this article, I will teach you about 3 different ways to copy a python list. In python 2, you can either use slicing or the list() function. In python 3, you can use the more elegant copy() method.
🌐
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