-1
segp2=[['13s'],['3c','6h','8h','9h','10c','11h'],['4c','4h','5c','5h','7c','7s'],[],[],[],['4h','5h','6h','8h','9h','11h'],[],[]]

copyseg=list(segp2)
copy=list(copyseg[6]);x=0;y=0
for i in copyseg[1]:
    if i in copyseg[6]:copyseg[1].remove(i);copy.remove(i);x+=1
    if x==5:break
for i in copyseg[2]:
    if i in copyseg[6]:copyseg[2].remove(i);copy.remove(i);x+=1
    if x==5:break
for i in copyseg[3]:
    if i in copyseg[6]:copyseg[3].remove(i);copy.remove(i);x+=1
    if x==5:break
print(segp2)
print(copyseg)

These 2 are somehow coming out the same, even though segp2 is literally untouched. Using 'copyseg=list(segp2)' in my experience makes a copy of the list that is unaffected by new code. Yet somehow, everything I do on the 'copyseg' list is copied onto the 'segp2' list. I've tried everything I can think of and can't figure out why it's doing this.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
raindd
  • 1
  • 1

1 Answers1

0

You should do a deep copy of the seg2 list:

import copy

copyseg = copy.deepcopy(segp2)

or, if your list is only one-level deep,

copyseg = [list(lst) for lst in segp2]
enzo
  • 9,861
  • 3
  • 15
  • 38