Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python List Issue
#4
list1 and list2 are the same list, not copies. As I said in my previous post if you want to copy a list you need to use copy().
list1=["a","b","c"]
list2=list1.copy()
list1.remove("a")
print("list1 = ",list1)
print("list2 = ",list2)
Variables in Python are really just names used to reference objects, not the objects themselves. You can see the python object using id(var) to see the actual python object referenced by a variable. In the code below, list2 = list1 results in both variables referencing the same list object. Printing the id's shows they are the same. list2=list1.copy() creates a new list object which is a copy of list1. When the id's are printed you can see they are different.
list1 = ["a", "b", "c"]
list2 = list1
print("Same.  list1", id(list1), "list2", id(list2))
list2.remove("a")
print("list1 = ", list1)
print("list2 = ", list2)

list1 = ["a", "b", "c"]
list2 = list1.copy()
print("Copy.  list1", id(list1), "list2", id(list2))
list2.remove("a")
print("list1 = ", list1)
print("list2 = ", list2)
Output:
Same. list1 2106770607872 list2 2106770607872 list1 = ['b', 'c'] list2 = ['b', 'c'] Copy. list1 2106770607360 list2 2106770612480 list1 = ['a', 'b', 'c'] list2 = ['b', 'c']
You only have to worry about copies when the copying mutable objects. A mutable object is one that can be changed after it is created. Mutable Python types include: list, dictionary and set. Python strings (str) are immutable (cannot be changed). Making copies of immutable types makes no sense because the objects cannot be changed. Everyone can use the same object without fear that it will be modified somewhere else.

list1.copy() makes a "shallow copy". It creates a new list, but the new list contains the same Python objects as list1. If list1 was a list of mutable objects, and you wanted the copied list to contain copies of the original list's contents, you need to do a "deep copy". Use the Python copy library functions to make a deep copy.

https://docs.python.org/3/library/copy.html
Reply


Messages In This Thread
Python List Issue - by Aggie64 - Jun-29-2022, 02:15 PM
RE: Python List Issue - by deanhystad - Jun-29-2022, 02:50 PM
RE: Python List Issue - by Aggie64 - Jun-30-2022, 02:15 PM
RE: Python List Issue - by deanhystad - Jun-30-2022, 04:35 PM
RE: Python List Issue - by perfringo - Jun-30-2022, 05:02 PM
RE: Python List Issue - by Aggie64 - Jun-30-2022, 09:15 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  List Comprehension Issue johnywhy 5 643 Jan-14-2024, 07:58 AM
Last Post: Pedroski55
  List to table issue robdineen 2 1,513 Nov-07-2021, 09:31 PM
Last Post: robdineen
  Calculator code issue using list kirt6405 4 2,353 Jun-11-2021, 10:13 PM
Last Post: topfox
  Issue accessing data from Dictionary/List in the right format LuisSatch 2 2,299 Jul-25-2020, 06:12 AM
Last Post: LuisSatch
  For List Loop Issue Galdain 2 2,118 Dec-31-2019, 04:53 AM
Last Post: Galdain
  IndexError: List index out of range issue Adem 1 3,584 Nov-01-2019, 10:47 PM
Last Post: ichabod801
  List/String seperation issue YoungGrassHopper 13 5,707 Sep-20-2019, 11:57 AM
Last Post: perfringo
  List Issue Batman 3 2,711 Jun-06-2019, 11:56 PM
Last Post: Batman
  Basic List Issue rogueakula 1 2,211 May-18-2019, 06:01 PM
Last Post: snippsat
  List slicing issue Irhcsa 3 3,059 Apr-26-2019, 09:16 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

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