Below is the code I wrote to generate all permutations from a list.
def perm(arr):
if len(arr)==1:
return (arr)
if len(arr)==0:
return ([])
else:
result=[]
for i in range(len(arr)):
x=arr[i]
xs=arr[:i]+arr[i+1:]
for p in perm(xs):
result.append([x]+p)
return (result)
perm(['a', 'b', 'c'])
I got error below:
TypeError: can only concatenate list (not "str") to list
I spent a long time trying to figure out why, but I could not. Can anyone help with why above code give those error? Thanks a lot in advance.