def permute(l, post): # Base case if l == []: return [post] ret = [] # Set ind to 0, 1, 2 ... # Set i to l[0], l[1], l[2] ... for ind, i in enumerate(l): # n is l without the current value (everything with an index < # ind + everything with an index > ind) n = l[:ind] + l[ind + 1:] # m is just post with i at the end m = post + [i] # Permute some more! ret += permute(n, m) # Since the lists we've been making aren't sorted like in the C++ # version ret.sort() # Now return it return ret