with open("input.txt") as f: all_crabs = [int(i) for i in f.read().strip().split(",")] # all_crabs = [int(i) for i in "16,1,2,0,4,2,7,1,2,14".split(",")] tries = [] for i in range(0, len(all_crabs)): moves = 0 for crab in all_crabs: moves += abs(i - crab) tries.append((i, moves, )) from pprint import pprint tries.sort(key=lambda x: x[1], reverse=True) pprint(tries)