# Easy List question

Abe Mathews 59Bassman at gmail.com
Tue Oct 5 17:03:26 CEST 2004

```I apologize in advance as I'm new to python, and relatively new to
programming.  However, I took this as a bit of a challenge this
morning to see if I could come up with another method for generating
the permutations of a list.

This is longer than what you proposed, but it makes sense to me and it
doesn't use the "list" name which I believe is reserved in Python.
Permute should return a list containing all of the permutations of the
list that you have given it.

Abe Mathews
------------------------------------------

def permute(orig_list):
newlist = [] # this will be a list of permutated lists
modlist = orig_list[:] # make a copy so we don't mess with the original
len_l = len(orig_list)

for i in range(0,len_l):
if len_l==1:
return [orig_list]
else:
list_tail = permute(modlist[1:])
for c in list_tail:
c.insert(0,modlist[0])
newlist.extend(list_tail)
new_end = modlist.pop(0)
modlist.extend(new_end)

return newlist

l = ["a","b","c","d"]
s = permute(l)

for i in s:print i
print len(s)

On 5 Oct 2004 04:39:49 -0700, Matt Williams <matt at mwilliams.org> wrote:
> Dear list,
>
> Can anyone explain why the append method here returns an 'unpermuted'
> list (and how to solve it_?
>
> I'm stuck?
>
> s=[]
> def permute(list):
>     len_l = len(list)
>     if len_l == 1:
>         print l
>         s.append(l)
>     for i in range (0, len_l):
>         permute(list[1:])
>         # now change position of first element with next (rotate will
> do it?)
>         list.append(list.pop(0))
>         # reflect this change in the main list
>         l[len(l) - len_l:] = list
>
> l=["a","b","c"]
>
> permute(l)
> --
> http://mail.python.org/mailman/listinfo/python-list
>

```