combinations of variable length nested lists

Just van Rossum just at letterror.com
Tue Aug 7 15:41:39 EDT 2001


I wrote:

> This one uses list comprehensions (it's based on David Ullrich's version):
> 
>[ ..snipped recursive version.. ]

It turns out a non-recursive version was easier than I thought (also using
list comprehensions):


input = [[1, 2], [4, 5, 6], [8, 11]]

def permute(alist):
    results = [[]]
    for i in range(len(alist)):
        results = [res + [item] for res in results for item in alist[i]]
    return results

all = permute(input)
for x in all:
    print x
print


Just



More information about the Python-list mailing list