How to concatenate list members

Ruediger Maehl ruediger.maehl_nospam at web.de
Thu May 30 08:26:54 EDT 2002


"Eric Brunel" <eric.brunel at pragmadev.com> wrote
> Try this:
>
> def concat1(alist, sep=" "):
>   if not alist: return ''
>   return reduce(lambda x,y,sep=sep: x + sep + y, alist)
>
> The "if" is necessary because as it's written, the reduce won't work on
> empty lists.
>
> If you like one-liners, you can also do this:
>
> b = (a and reduce(lambda x,y: x + "_" + y, a)) or ''
>
> HTH
> --
> - Eric Brunel <eric.brunel at pragmadev.com> -
> PragmaDev : Real Time Software Development Tools -
http://www.pragmadev.com

Hello Eric,

Thanks for your solution (although I don't understand it).

Rüdiger





More information about the Python-list mailing list