ANOTHER APPROACH (without libs)
def permutation(input):
if len(input) == 1:
return input if isinstance(input, list) else [input]
result = []
for i in range(len(input)):
first = input[i]
rest = input[:i] + input[i + 1:]
rest_permutation = permutation(rest)
for p in rest_permutation:
result.append(first + p)
return result
Input can be a string or a list
print(permutation('abcd'))
print(permutation(['a', 'b', 'c', 'd']))