Can you set the indexes of the indices as variables in the original list?

I am trying to print a list of integers, one element per line, after replacing the position of the largest element with the last element. After replacing items, they are still printed in their original position.

large = values[values.index(max(values))]
last = values[-1]

large, last = last, large

for i in values:
    print i

      

I apologize if this has already been answered, I have not yet been able to find it.

+3


source to share


2 answers


You can contact him:

max_index = values.index(max(values))
values[max_index], values[-1]= values[-1], values[max_index]

      



As you type, large, last = last, large

you need to understand that large and last are variables that are separate from the list values. means that even if you change them, the list will not change.

To update the list, you need to update the values ​​inside the list as above.

+6


source


You need to remap the big and the last one where you want them to be in the list. After you swap, add this:



values[values.index(max(values))] = large
values[-1] = last

      

+2


source







All Articles