Combinations of n Lists

Fredrik Lundh fredrik at pythonware.com
Wed Feb 28 18:05:35 CET 2001


Sean 'Shaleh' Perry wrote:
> > 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')]
> >
>
> try zip() in python2 (-:

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

Cheers /F





More information about the Python-list mailing list