Combinations of n Lists

Warren Postma embed at geocities.com
Wed Feb 28 10:20:46 EST 2001


I was just wondering if anyone has a more general version of this little
helper function:

def combinations(list1,list2):
    return [ (i,j) for i in list1 for j in list2 ]

print combinations( [1,2,3], ['a','b','c'] )

[(1, 'a'), (1, 'b'), (1, 'c'), (2, 'a'), (2, 'b'), (2, 'c'), (3, 'a'), (3,
'b'), (3, 'c')]

So, what if I want

def combinations(*lists):
        ....
print combinations( [1,2,3], [4,5,6], [7,8,9], .... )
    [ [1,4,7,....],  .... [1,4,8, ... ], ..... ]

Any takers!? ;-)

Warren





More information about the Python-list mailing list