pairwise combination of two lists

Mel mwilson at the-wire.com
Wed Aug 17 23:18:40 CEST 2011


Mel wrote:

> Yingjie Lin wrote:
>> I have two lists:
>> 
>> li1 = ['a', 'b']
>> li2 = ['1', '2']
>> 
>> and I wish to obtain a list like this
>> 
>> li3 = ['a1', 'a2', 'b1', 'b2']
[ ... ]
> This seems to do it :
> 
> mwilson at tecumseth:~$ python
> Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56)
> [GCC 4.4.3] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>>>> import itertools
>>>> li1 = ['a', 'b']
>>>> li2 = ['1', '2']
>>>> map (lambda (x,y):x+y, list (itertools.product (li1, li2)))
> ['a1', 'a2', 'b1', 'b2']


I have doubts about this in Python3, since tuple unpacking in a argument 
list isn't done there, and I don't think sum works on strings.  Some other 
function can probably be made to work.

	Mel.



More information about the Python-list mailing list