pairwise combination of two lists

Gary Herron gherron at islandtraining.com
Thu Aug 18 03:26:18 CEST 2011


On 08/17/2011 01:22 PM, Yingjie Lin wrote:
> Hi Python users,
>
> I have two lists:
>
> li1 = ['a', 'b']
> li2 = ['1', '2']
>
> and I wish to obtain a list like this
>
> li3 = ['a1', 'a2', 'b1', 'b2']
>
> Is there a handy and efficient function to do this, especially when li1 and li2 are long lists.
> I found zip() but it only gives [('a', '1'), ('b', '2')],  not exactly what I am looking for.
>
> Thank you.
>
>
> - Yingjie
 >>> li1 = ['a', 'b']
 >>> li2 = ['1', '2']
 >>> print [a+b   for a in li1   for b in li2]
['a1', 'a2', 'b1', 'b2']


Gary Herron



More information about the Python-list mailing list