Since it looks like you were trying to come up with a recursive solution, here is the general form of it:
def all_perms(thing):
if len(thing) <=1:
yield thing
else:
for perm in all_perms(thing[1:]):
for i in range(len(perm)+1):
yield perm[:i] + thing[0:1] + perm[i:]
This works for most kinds of iterables. Demo:
In [5]: list(all_perms(('shirt','tie','suit')))
Out[5]:
[('shirt', 'tie', 'suit'),
('tie', 'shirt', 'suit'),
('tie', 'suit', 'shirt'),
('shirt', 'suit', 'tie'),
('suit', 'shirt', 'tie'),
('suit', 'tie', 'shirt')]
Recursion is difficult to understand at first, but the general form is:
if simplest_case:
return simplest_case
else:
#recurse
In this case, return
is replaced by yield
in order to make a generator, which is more memory-friendly. You still shouldn't expect this to be the best-performing solution, but I'm including it for completeness since "USE ITERTOOLS" doesn't end up teaching you much other than itertools is cool.