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


Groups > comp.lang.python > #11717 > unrolled thread

Re: pairwise combination of two lists

Started byNed Deily <nad@acm.org>
First post2011-08-17 14:33 -0700
Last post2011-08-17 14:33 -0700
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: pairwise combination of two lists Ned Deily <nad@acm.org> - 2011-08-17 14:33 -0700

#11717 — Re: pairwise combination of two lists

FromNed Deily <nad@acm.org>
Date2011-08-17 14:33 -0700
SubjectRe: pairwise combination of two lists
Message-ID<mailman.147.1313616814.27778.python-list@python.org>
In article <98CC6556-11F3-4850-BD2B-30481B53042D@mssm.edu>,
 Yingjie Lin <Yingjie.Lin@mssm.edu> 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.

>>> from itertools import product
>>> li1 = ['a', 'b']
>>> li2 = ['1', '2']
>>> li3 = list("".join(x) for x in product(li1, li2))
>>> li3
['a1', 'a2', 'b1', 'b2']

-- 
 Ned Deily,
 nad@acm.org

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web