from itertools import permutations def I(tupPerm): tmpList=[] for k in tupPerm: tmpL=list(tupPerm) kIndex=tmpL.index(k) copyTup=[x for x in tmpL] copyTup2=copyTup[(kIndex+1):] ctInversionsForKList=[] for x in copyTup2: if x