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


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 547 Jan-14-2024, 07:58 AM
Last Post: Pedroski55
  List to table issue robdineen 2 1,473 Nov-07-2021, 09:31 PM
Last Post: robdineen
  Calculator code issue using list kirt6405 4 2,298 Jun-11-2021, 10:13 PM
Last Post: topfox
  Issue accessing data from Dictionary/List in the right format LuisSatch 2 2,229 Jul-25-2020, 06:12 AM
Last Post: LuisSatch
  For List Loop Issue Galdain 2 2,061 Dec-31-2019, 04:53 AM
Last Post: Galdain
  IndexError: List index out of range issue Adem 1 3,550 Nov-01-2019, 10:47 PM
Last Post: ichabod801
  List/String seperation issue YoungGrassHopper 13 5,542 Sep-20-2019, 11:57 AM
Last Post: perfringo
  List Issue Batman 3 2,661 Jun-06-2019, 11:56 PM
Last Post: Batman
  Basic List Issue rogueakula 1 2,196 May-18-2019, 06:01 PM
Last Post: snippsat
  List slicing issue Irhcsa 3 2,994 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