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
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
python - Create a copy of the list not referencing the contained objects - Stack Overflow
I have learned that to create a copy of the object a without referencing it I should use the following syntaxis: ... So it's clear to me that the object a[0] is being referenced even if contained in a. How can I create a copy of the object a in a way that even all its internal objects will not be referenced? ... Save this answer. ... Show activity on this post. ... You can use the copy.deepcopy() function to recursively copy the objects, or use a list ... More on stackoverflow.com
🌐 stackoverflow.com
I just found out I've been copying lists wrong all along.
You should do some_numbers = other_numbers.copy() To get a fresh instance of the list. More on reddit.com
🌐 r/learnpython
148
677
September 17, 2020
🌐
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.
🌐
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.
Find elsewhere
🌐
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....
🌐
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.
🌐
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().
🌐
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.
🌐
Sentry
sentry.io › sentry answers › python › clone a list in python
Clone a List in Python With Shallow and Deep Copy | Sentry
November 15, 2023 - 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.
🌐
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, ...
🌐
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 :-)

🌐
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.
🌐
Reddit
reddit.com › r/learnpython › i just found out i've been copying lists wrong all along.
r/learnpython on Reddit: I just found out I've been copying lists wrong all along.
September 17, 2020 -

In order to explain this, assume we have two lists: some_numbers and other_numbers, where

>>> some_numbers = [1, 2, 3, 4, 5]
>>> other_numbers = some_numbers

Many beginners would make a copy of some_numbers and store it in other_numbers, as I did above. However, what you might not know, is that when you do that you actually tell Python that they are the same list. So if you tried to add an item to some_numbers and then print(other_numbers), you'd see that the output will include the item you added to some_numbers. i.e.:

>>> some_numbers.append(10)
>>> print(other_numbers)
[1, 2, 3, 4, 5, 10]

If you wish to have two independent lists, where adding an item to one doesn't change the other, use this instead:

>>> some_numbers = [1, 2, 3, 4, 5]
>>> other_numbers = some_numbers[:]

Now, you can change the lists independently.

>>> some_numbers.append(10)
>>> other_numbers.append(20)
>>> print(some_numbers)
>>> print(other_numbers)
[1, 2, 3, 4, 5, 10]
[1, 2, 3, 4, 5, 20]

Edit: As mentioned in the comments by several people, the more readable option for copying is

>>> some_numbers = [ 1, 2, 3, 4, 5]
>>> other_numbers = some_numbers.copy()

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