1

I'm currently stuck at this problem, so i have this code.

     rij1 = rij2
     rij2.clear()

All works correct, but in the end of the loop is a problem. Rij1 also gets cleared because apparently, python remembers rij1 = rij2 Is there any way to prevent this?

braX
  • 11,506
  • 5
  • 20
  • 33
Eric mansen
  • 141
  • 2
  • 15

2 Answers2

2

rij1 = rij2 stores the same reference of list at rij2 to rij1. For storing the new copy of list, you should do:

rij1 = rij2[:]

# OR
rij1 = list(rij2)

Also, you may use copy.copy to create shallow copy of list as:

from copy import copy

rij1 = copy(rij2)
Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126
1

You need to make a copy of the list. After rij1 = rij2, both variables are names for the same list.

Something like

rij1 = rij2[:]

will work.

RemcoGerlich
  • 30,470
  • 6
  • 61
  • 79