I want to write a recursive function p() that takes a list and returns a list of all permutations of the input list.
Ex.
>>>permutations([1, 2,3])
[[1, 2,3],[2, 1, 3],[2, 3, 1],[1, 3, 2],[3, 1, 2],[3, 2, 1]]
I want to recursively call on a sublist l[1:] for all permutations of all elements of the original input list except the first element, l[0], and then generate the permutations of the original list by adding l[0] to those permutations.
So far, I have
def p(list):
if len(list)==1 or 0:
return list
result = []
for i in list[1:]:
result.append(i + list[0])
result += [list]
return result
But I know something is wrong.... help please?