-4

Here is my code:

x,y= int(input()),list(map(int, input().split(" "))) ; y_2=y
for i in range(int(x/2)): 
    print(y_2.index(min(y)),y_2.index(max(y)))
    del y[y.index(min(y))]
    del y[y.index(max(y))]

When I checked y, it changed, which is what I want. But then, my backup list y_2 also changed. why is that?

Dav_Did
  • 188
  • 10
  • This just makes to variables point to the same list: `y_2=y` it doesn't make a "backup". You could try `y_2 = y.copy()`. – Mark Dec 27 '20 at 02:40

1 Answers1

0

It is because you set y_2 to be the same list as y. That list is an object, and you created two references to that object. If you want to create an independent copy of the list you could use y_2 = list(y), or use the copy module in more complicated cases.

Btw please just change that first line in to three lines; it would be a tremendous increase of readability.

simon
  • 796
  • 1
  • 6
  • 11