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 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
python - How do I clone a list so that it doesn't change unexpectedly after assignment? - Stack Overflow
See the Python FAQ. ... new_list = my_list doesn't actually create a second list. The assignment just copies the reference to the list, not the actual list, so both new_list and my_list refer to the same list after the assignment. More on stackoverflow.com
🌐 stackoverflow.com
How do I copy a list without replacing the original one?
https://docs.python.org/2/library/copy.html More on reddit.com
🌐 r/learnpython
6
1
October 12, 2017
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
🌐
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]]
🌐
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.
🌐
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)
For example, the statement ai_models ... changes made to one list also affects the other. ... As mentioned above, using = without copy() makes a variable reference the same list without creating a copy....
Find elsewhere
🌐
Dataquest
dataquest.io › home › blog › how to copy a list in python (5 techniques w/ examples)
How to Copy a List in Python (5 Techniques w/ Examples)
March 6, 2023 - In this section, we'll learn about a built-in method that Python programmers commonly use for copying lists. The Python copy() method returns a shallow copy of the list without taking any parameters.
🌐
Unstop
unstop.com › home › blog › copy python lists | shallow & deep copies (+code examples)
Copy Python Lists | Shallow & Deep Copies (+Code Examples)
January 7, 2025 - Ready to upskill your Python knowledge? Dive deeper into actual problem statements and elevate your coding game with hands-on practice! To copy a list without references to the original, you need to create a deep copy.
🌐
Stanford CS
cs.stanford.edu › people › nick › py › python-nocopy.html
Python No Copy / is
No, like before, there is never a copy. The called function exclaim() just gets a reference to the list that caller created.
Top answer
1 of 1
4159

new_list = my_list doesn't actually create a second list. The assignment just copies the reference to the list, not the actual list, so both new_list and my_list refer to the same list after the assignment.

To actually copy the list, you have several options:

  • You can use the built-in list.copy() method (available since Python 3.3):

    new_list = old_list.copy()
    
  • You can slice it:

    new_list = old_list[:]
    

    Alex Martelli's opinion (at least back in 2007) about this is, that it is a weird syntax and it does not make sense to use it ever. ;) (In his opinion, the next one is more readable).

  • You can use the built-in list() constructor:

    new_list = list(old_list)
    
  • You can use generic copy.copy():

    import copy
    new_list = copy.copy(old_list)
    

    This is a little slower than list() because it has to find out the datatype of old_list first.

  • If you need to copy the elements of the list as well, use generic copy.deepcopy():

    import copy
    new_list = copy.deepcopy(old_list)
    

    Obviously the slowest and most memory-needing method, but sometimes unavoidable. This operates recursively; it will handle any number of levels of nested lists (or other containers).

Example:

import copy

class Foo(object):
    def __init__(self, val):
         self.val = val

    def __repr__(self):
        return f'Foo({self.val!r})'

foo = Foo(1)

a = ['foo', foo]
b = a.copy()
c = a[:]
d = list(a)
e = copy.copy(a)
f = copy.deepcopy(a)

# edit orignal list and instance 
a.append('baz')
foo.val = 5

print(f'original: {a}\nlist.copy(): {b}\nslice: {c}\nlist(): {d}\ncopy: {e}\ndeepcopy: {f}')

Result:

original: ['foo', Foo(5), 'baz']
list.copy(): ['foo', Foo(5)]
slice: ['foo', Foo(5)]
list(): ['foo', Foo(5)]
copy: ['foo', Foo(5)]
deepcopy: ['foo', Foo(1)]
🌐
Sentry
sentry.io › sentry answers › python › clone a list in python
Clone a List in Python With Shallow and Deep Copy | Sentry
1 week ago - Create independent copies of Python lists using list.copy, slicing, the list constructor for shallow copies, or copy.deepcopy for nested structures
🌐
pythontutorials
pythontutorials.net › blog › copy-a-list-of-list-by-value-and-not-reference
How to Copy a List of Lists by Value (Not Reference) in Python: Avoid Unintended Changes in Matrix Operations — pythontutorials.net
In Python, variables store references to objects, not the objects themselves. For mutable objects like lists, this means: Assigning a list to a new variable creates a reference to the original list, not a copy.
🌐
Reddit
reddit.com › r/learnpython › how do i copy a list without replacing the original one?
r/learnpython on Reddit: How do I copy a list without replacing the original one?
October 12, 2017 -

Hey r/learnpython

I am having trouble copying a list from an imported script I made without changing the original list. I tried to use this code:

def spawn_npcs():
    for npc in SETTINGS.levels_list[SETTINGS.current_level].npcs:
        try:
            stats = [x for x in SETTINGS.npc_types if x[0]['id'] == npc[2]][0].copy()
            print(stats[0]['pos'], SETTINGS.npc_types[0][0]['pos'])
            stats[0]['pos'] = npc[0]
        except:
            print("ERROR: No NPC with ID = ", npc[2])

        print(stats[0]['pos'], SETTINGS.npc_types[0][0]['pos'])

It prints [0,0] [0,0] to the console and then [4,4] [4,4]. It also changed the list in SETTINGS.py - How do I avoid that?

Thanks :-)

🌐
Python Engineer
python-engineer.com › posts › how-to-copy-list
How to copy a List in Python without side effects - Python Engineer
January 12, 2023 - In this case, we have to make a deep copy to clone all inner elements, too. You can learn more about Shallow vs. Deep Copying in Python in this article. To make a full clone of the List and all its inner elements, we have to use copy.deepcopy().
🌐
W3Schools
w3schools.com › python › python_lists_copy.asp
Python - Copy Lists
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Practice Problems Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Training ... You cannot copy a list simply by typing list2 = list1, ...
🌐
Better Programming
betterprogramming.pub › how-to-clone-or-copy-a-list-in-python-9c3e15c92bf0
How To Clone or Copy a List in Python | by Jonathan Hsu | Better Programming
November 5, 2019 - Python · Jonathan Hsu · 2 min read · ·Oct 23, 2019 · -- 4 · Listen · Share · Press enter or click to view image in full size · Photo by Kelly Sikkema on Unsplash · Working with lists is a bit different than primitive data types because setting your new variable equal to the existing one will create a reference rather than a standalone copy.
🌐
Medium
medium.com › @thawsitt › assignment-vs-shallow-copy-vs-deep-copy-in-python-f70c2f0ebd86
Python: Assignment vs Shallow Copy vs Deep Copy | by Thawsitt Naing | Medium
May 7, 2019 - Use shallow copy [:] if you don’t want the new changes to affect the original list. For compound objects (e.g. a list of lists): Use assignment = if you want the new changes to affect the original list.
🌐
Medium
medium.com › analytics-vidhya › three-ways-to-copy-list-items-in-python-5471ec45c3b4
Six ways to Copy List items in Python | by Bhadresh Savani | Analytics Vidhya | Medium
February 26, 2024 - Let's have look at it. ... 1.Using the Copy() Method: the Copy method of the list object is used to copy only the content inside the list. So the new list will not have the same reference id or memory location.
🌐
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