pairwise combination of two lists
mwilson at the-wire.com
Wed Aug 17 17:15:34 EDT 2011
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']
> 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.
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']
More information about the Python-list