Count swaps needed to order a list of integers I wrote this code since I'm going through the hackerrank challenges, however, it takes way too long to run.
Its function is described on the title, but just to clear it up, we need to count the minimum amount of swaps needed to order a set of ascending integers.

def minimumSwaps(arr):
    count = 0
    correct = [(x+1) for x in range(len(arr))]
    while arr != correct:
        for i in arr:
            if i != arr.index(i)+1:
                a = i
                b = arr[i-1]
                arr[i-1] = a
                arr[arr.index(i)] = b
                count += 1
                break
    return count
if __name__ == '__main__':
    n = int(input())
    arr = list(map(int, input().rstrip().split()))
    res = minimumSwaps(arr)
    print(str(res))
I don't have time to really look at the code well or do a full review, but what did stick out to me was the repeated calls to arr.index(i). That's an expensive call that will require, in the worst case, a full iteration of arr each time.

At the very least, that call should be done once then cached. Really though, that information is already available to you without searching if you use enumerate:

def minimum_swaps(arr):
    count = 0
    correct = [(x+1) for x in range(len(arr))]
    while arr != correct:
        for i, x in enumerate(arr):  # I changed what was i to x, then made i the index
            if x != i+1:
                a = x
                b = arr[x-1]
                arr[x-1] = a
                arr[i] = b
                count += 1
                break

    return count


I also renamed some variables. i is an awful name for an element that isn't itself an index, and the function name should be in snake_case instead.