Permutation Generator

Talin talin at acm.org
Fri Aug 12 15:39:08 EDT 2005


I'm sure I am not the first person to do this, but I wanted to share 
this: a generator which returns all permutations of a list:

def permute( lst ):
    if len( lst ) == 1:
        yield lst
    else:
        head = lst[:1]
        for x in permute( lst[1:] ):
            yield head + x
            yield x + head
    return

-- Talin



More information about the Python-list mailing list