Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Copy List Not Copying
#1
This works:
A=[1]
B=A.copy()
print(B)
print(A)
print(B)
B.append(2)
print(B)
print(A)
print(B)
This does not:
A=[[1]]
B=A.copy()
print(B)
print(A)
print(B)
B[0].append(2)
print(B)
print(A)  # Bad A here.
print(B)
A is getting modified. Any reason why?

Thanks,
Bassam
Larz60+ write Aug-19-2023, 01:06 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Tags added. Please use BBCode tags on future posts.
Reply
#2
I'm assuming the inner lists for B are pointers, but why? Do I need to loop through each item to create a copy?
Reply
#3
This is a mutable vs immutable thing. Everything in Python is an object. Some objects, like numbers and strings, are immutable, they cannot be modified. 2 is always 2 and "cat" is always "cat". Other objects, like lists and dictionaries, are mutable, they can be changed after they are created.

When you copy a list, a new list is created that contains the same objects as the old. B = A.copy() creates a new list that is assigned to B. The contents of list B are the same objects as those in list A.

In your first example, A contains a number, an immutable object. When you copy A, the new list contains the same object, an immutable number.

In the second example, A contains a list, [1]. When you copy A, the new list contains the same object as A, [1]. Both A and B contain the same list object. If you change that object it will look like both A and B changed, but the didn't. A abd B are the same as before, only the list object they have in common changed.
BAbdulBaki likes this post
Reply
#4
You can start with interactive interpreter:

>>> help(list.copy)
Help on method_descriptor:

copy(self, /)
    Return a shallow copy of the list.
So the copy is shallow. What does it mean? From Python documentation:

Quote:Assignment statements in Python do not copy objects, they create bindings between a target and an object. For collections that are mutable or contain mutable items, a copy is sometimes needed so one can change one copy without changing the other.

/.../

The difference between shallow and deep copying is only relevant for compound objects (objects that contain other objects, like lists or class instances):

- A shallow copy constructs a new compound object and then (to the extent possible) inserts references into it to the objects found in the original.
- A deep copy constructs a new compound object and then, recursively, inserts copies into it of the objects found in the original.

Two problems often exist with deep copy operations that don’t exist with shallow copy operations:

- Recursive objects (compound objects that, directly or indirectly, contain a reference to themselves) may cause a recursive loop.
- Because deep copy copies everything it may copy too much, such as data which is intended to be shared between copies.
BAbdulBaki likes this post
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why is the copy method name in python list copy and not `__copy__`? YouHoGeon 2 287 Apr-04-2024, 01:18 AM
Last Post: YouHoGeon
  Copying the order of another list with identical values gohanhango 7 1,171 Nov-29-2023, 09:17 PM
Last Post: Pedroski55
  About list copy. water 3 1,558 Apr-03-2022, 02:42 AM
Last Post: deanhystad
Question Making a copy list in a function RuyCab 1 1,805 Jul-11-2021, 02:06 PM
Last Post: Yoriz
  Theory behind referencing a dictionary rather than copying it to a list sShadowSerpent 2 2,090 Mar-24-2020, 07:18 PM
Last Post: sShadowSerpent
  Copying a file Kundan 1 2,108 Aug-21-2019, 09:55 AM
Last Post: snippsat
  Copy List Structure leoahum 2 2,897 Mar-22-2019, 05:40 PM
Last Post: leoahum
  Copying classes in a list marienbad 1 2,645 Oct-14-2018, 04:42 PM
Last Post: ichabod801
  copy list into variable poroton 1 2,611 Aug-10-2018, 07:19 AM
Last Post: Gribouillis
  Copy List [Help Needed] Patricamillie 0 2,227 Jun-11-2018, 10:53 AM
Last Post: Patricamillie

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020