combinations of variable length nested lists

xauau xauau at
Wed Aug 8 12:16:40 CEST 2001

"Joseph A. Knapka" <jknapka at> wrote in message news:<3B700C33.1D161BAC at>...

> PEP proposal: add a backtracking Horn-clause resolution

> engine to Python!


>  :-)


> This problem is a two-liner in Prolog:


> one_of_each([],[]).

> one_of_each([H|T],[H1|Ts]) :- member(H1,H), one_of_each(T,Ts).

It is in Python too:

def permute(a):
    if len(a) == 0: return [[]]
    return [[x] + y for x in a[0] for y in permute(a[1:])]

Actually, it needs all of three lines if you count the func def ;-)

Just for fun, does anyone know how to move the 'if' into the list
comprehension to make it a one-liner?

More information about the Python-list mailing list