Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #11718

Re: pairwise combination of two lists

From Kev Dwyer <kevin.p.dwyer@gmail.com>
Subject Re: pairwise combination of two lists
Followup-To gmane.comp.python.general
Date 2011-08-17 22:33 +0100
References <98CC6556-11F3-4850-BD2B-30481B53042D@mssm.edu>
Newsgroups comp.lang.python
Message-ID <mailman.148.1313616838.27778.python-list@python.org> (permalink)

Followups directed to: gmane.comp.python.general

Show all headers | View raw


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

Hello 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']


Cheers,

Kev

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: pairwise combination of two lists Kev Dwyer <kevin.p.dwyer@gmail.com> - 2011-08-17 22:33 +0100

csiph-web