import sys digits = [ x for x in range(0, 10) ] limit = 1000000 count = 0 def permutate(start, end): global permutations, count if len(end) == 1: count += 1 if count == limit: print start+end sys.exit(0) return for i in range(0, len(end)): s = start + [end[i]] e = end[:i] + end[i+1:] permutate(s, e) permutate([], digits)