newbie - concatanating 2 lists

Alex Martelli aleaxit at yahoo.com
Thu Feb 22 03:47:00 EST 2001


"Steve Purcell" <stephen_purcell at yahoo.com> wrote in message
news:mailman.982777311.18345.python-list at python.org...
> Gnanasekaran Thoppae wrote:
> > i am just beginning to use python.
>
> Good choice!
>
> > li1 = ['a', 'b', 'c']
> > li2 = ['x', 'y', 'z']
> >
> > i want:
> >
> > li3 = ['ax', 'by', 'cz']
>
> There may be more readable ways, but the shortest is probably this:
>
> >>> li1 = ['a', 'b', 'c']
> >>> li2 = ['x', 'y', 'z']
> >>> li3 = map(lambda a,b: a+b, ['a','b','c'], ['x','y','z'])
> >>> li3
> ['ax', 'by', 'cz']

It's not the shortest way in Python2 -- rather, I'd code it:

      li3 = [ a+b for a,b in zip(li1, li2) ]


Alex






More information about the Python-list mailing list