pairwise combination of two lists
kevin.p.dwyer at gmail.com
Wed Aug 17 17:33:34 EDT 2011
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
This isn't exactly handy, but...
>>> import itertools
>>> a = ('a', 'b')
>>> b = (1, 2)
>>> [x + str(y) for (x, y) in itertools.product(*(a, b))]
['a1', 'a2', 'b1', 'b2']
More information about the Python-list