I am doing an exercise to print out all the permutations of [0,1,2,3]. And the output is supposed to be in the same form, like [2,3,1,0] for example. I wrote down the following code.
def permutations(units):
permutation_list = []
if len(units) == 1:
return units
else:
for i in units:
new = [j for j in units if j != i]
[permutation_list.append([i] + p) for p in permutations(new)]
return permutation_list
print(permutations([0,1,2,3]))
However, it gives me an error saying that p is int and that list [i] and int p can not be added.
units is list and output is a list so I don't understand how p can be int. Any clarification would be very much appreciated.
Edit. This is the error I get:
[permutation_list.append([i] + p) for p in permutations(new)]
TypeError: can only concatenate list (not "int") to list