# Cannot get the right output for swapping in list (python)

```def largestPermutation(k, arr):
for i in range(k):
arr[i],arr[arr.index(max(arr[i:]))]=arr[arr.index(max(arr[i:]))],arr[i]
return arr

a =[4,2,3,5,1]
print(largestPermutation(1,a))
```

This is my code to return the largest permutation in a list with k swaps. I tried the Pen and paper method and got the code as follows but, the answer returns the same array I passed.

This is a HackerRank problem (Link : https://www.hackerrank.com/challenges/largest-permutation/problem)

The problem is that in the destination of the assignment, `arr[arr.index(max(arr[i:])]` is calculated after `arr[i]` has been assigned. So the maximum element has already been moved into the front position, so this will always return the index that you’re swapping into and it gets its original value back.

Assign the index of the max element to a variable before doing the swap.

```def largestPermutation(k, arr):
for i in range(k):
max_index = arr.index(max(arr[i:]))
arr[i],arr[max_index]=arr[max_index],arr[i]
return arr

a =[4,2,3,5,1]
print(largestPermutation(1,a))
```