3

I'm trying to print a list of integers, one item per line, after swapping the position of the largest item with the last item. After swapping the items, they still print in their original position.

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

large, last = last, large

for i in values:
    print i

I'm sorry if this has already been answered, I haven't been able to find it yet.

CoffeeCat
  • 35
  • 6
  • @Chris_Rands I had looked at that before I posted my question and I wasn't able to fix my problem based on that. – CoffeeCat Jul 03 '17 at 15:19

2 Answers2

6

You can reach it by:

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

When you typed large, last = last, large you need to understand that large and last are variables that separated from the list values. means that even if you change them, the list won't change.

In order to update the list, you need to update the values inside the list as typed above.

omri_saadon
  • 10,193
  • 7
  • 33
  • 58
3

You need to reassign large and last to where you would like them to be located in the list. After you make the swap add this:

values[values.index(max(values))] = large
values[-1] = last
Pierre Delecto
  • 455
  • 1
  • 7
  • 26