Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python List Issue
#1
1. create a list named All_Items_List
2. set All_Items_List_Copy = All_Items_List
3. create Subset_list which has one less item than All_Items_List and All_Items_List_Copy
4. remove all items from All_Items_List_Copy that exist in Subset_List
5 result is all items removed from All_Items_List_Copy are also removed from All_Items_List

I thought that after setting All_Items_List_Copy= All_Items_List, that All_Items_List_Copy would be separate from All_Items_List

Below is the code used:
All_Items_List = ["a","b","c","d","e","f","g","h"]
All_Items_List_Copy=All_Items_List
Subset_List =  ["a","b","c","d","e","f","g"]  #  "h" is missing from subset list

print("All_Items_List      = ",All_Items_List )
print("All_Items_List_Copy = ",All_Items_List_Copy)
print("Subset_List         = ",Subset_List)
print(" ")

for items in Subset_List :
    All_Items_List_Copy.remove(items)

print("All_Items_List     = ",All_Items_List )
print("All_Items_List_Copy = ",All_Items_List_Copy)
print("Subset_List         = ",Subset_List)

Output from the code:
All_Items_List      =  ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
All_Items_List_Copy =  ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
Subset_List         =  ['a', 'b', 'c', 'd', 'e', 'f', 'g']  # "h" is missing from the Subset_List
 
All_Items_List      =  ['h']
All_Items_List_Copy =  ['h']
Subset_List         =  ['a', 'b', 'c', 'd', 'e', 'f', 'g']
why does removing items from the All_Items_List_Copy also remove the same items from All_Items_List ?
Yoriz write Jun-29-2022, 03:42 PM:
Please post all code, output and errors (in their 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.
Reply
#2
Please wrap posted code in Python tags. There is a button in the editor for doing this.

Variables in Python are really just names used as convenient handles to reference Python objects. All_Items_List_Copy is a variable that references a list object, the same list object referenced by the variable All_Items_List. Use All_Items_List_Copy = All_Items_List.copy() if you want the two variables to reference different list objects.
Reply
#3
I simplified my code and used the forum standards.
Why is the item "a" removed from list2 when the item "a" is removed from list1?
list1=["a","b","c"]
list2=list1
list1.remove("a")
print("list1 = ",list1)
print("list2 = ",list2)
Code Output:
Output:
list1 = ['b', 'c'] list2 = ['b', 'c']
Reply
#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
#5
Gentle reminder of PEP8 Function and Variable Names:

Quote:Function names should be lowercase, with words separated by underscores as necessary to improve readability.

Variable names follow the same convention as function names.

mixedCase is allowed only in contexts where that’s already the prevailing style (e.g. threading.py), to retain backwards compatibility.

All_Items_List -> all_items_list
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
#6
Many thanks for your explanation. I am relatively new to Python and still have many things to learn
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  List Comprehension Issue johnywhy 5 440 Jan-14-2024, 07:58 AM
Last Post: Pedroski55
  List to table issue robdineen 2 1,435 Nov-07-2021, 09:31 PM
Last Post: robdineen
  Calculator code issue using list kirt6405 4 2,205 Jun-11-2021, 10:13 PM
Last Post: topfox
  Issue accessing data from Dictionary/List in the right format LuisSatch 2 2,169 Jul-25-2020, 06:12 AM
Last Post: LuisSatch
  For List Loop Issue Galdain 2 2,017 Dec-31-2019, 04:53 AM
Last Post: Galdain
  IndexError: List index out of range issue Adem 1 3,478 Nov-01-2019, 10:47 PM
Last Post: ichabod801
  List/String seperation issue YoungGrassHopper 13 5,357 Sep-20-2019, 11:57 AM
Last Post: perfringo
  List Issue Batman 3 2,608 Jun-06-2019, 11:56 PM
Last Post: Batman
  Basic List Issue rogueakula 1 2,152 May-18-2019, 06:01 PM
Last Post: snippsat
  List slicing issue Irhcsa 3 2,928 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