Python Forum

Full Version: Python List Issue
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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 ?
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.
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']
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
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
Many thanks for your explanation. I am relatively new to Python and still have many things to learn