def perms(L): if not L: return [[]] else: x = L[0] xs = L[1:] return [perm[:i] + [x] + perm[i:] for perm in perms(xs) for i in xrange(len(perm)+1)]