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


Groups > comp.lang.python > #11715

Re: pairwise combination of two lists

From Mel <mwilson@the-wire.com>
Newsgroups comp.lang.python
Subject Re: pairwise combination of two lists
Followup-To comp.lang.python
Date 2011-08-17 17:15 -0400
Organization Aioe.org NNTP Server
Message-ID <j2hb1k$ntf$1@speranza.aioe.org> (permalink)
References <mailman.146.1313614713.27778.python-list@python.org>

Followups directed to: comp.lang.python

Show all headers | View raw


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


	Mel.

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


Thread

pairwise combination of two lists Yingjie Lin <Yingjie.Lin@mssm.edu> - 2011-08-17 16:22 -0400
  Re: pairwise combination of two lists Mel <mwilson@the-wire.com> - 2011-08-17 17:15 -0400
    Re: pairwise combination of two lists Mel <mwilson@the-wire.com> - 2011-08-17 17:18 -0400
  Re: pairwise combination of two lists Marc Christiansen <usenet@solar-empire.de> - 2011-08-17 23:43 +0200
  Re: pairwise combination of two lists Luis M. González <luismgz@gmail.com> - 2011-08-17 16:27 -0700
  Re: pairwise combination of two lists Paul Rubin <no.email@nospam.invalid> - 2011-08-18 00:18 -0700
  Re: pairwise combination of two lists Alain Ketterlin <alain@dpt-info.u-strasbg.fr> - 2011-08-18 09:30 +0200
  Re: pairwise combination of two lists SigmundV <sigmundv@gmail.com> - 2011-08-18 14:51 -0700

csiph-web