How to concatenate list members
Christopher
chris_mk at hotmail.com
Thu May 30 16:03:59 EDT 2002
I have to agree that '"sep".join(map(str, somelist))' is the best
trade-off between simplicity and completeness (you never know when
something may not be a string). If you go to
http://www.mindview.net/Books/Python/ThinkingInPython.html
(Bruce Eckel's page), he has a section on the page called "An idiom
for concatenating strings" which looks like:
cs = lambda *args: ''.join(map(str,args))
with an example of:
print cs("X=",x," Y=",calc_y(x)) # you have to define calc_y somewhere
else.
If you do just look at it and work through it, it should make perfect
sense.
Chris
holger krekel <pyth at devel.trillke.net> wrote in message news:<mailman.1022760904.18161.python-list at python.org>...
> Ruediger Maehl wrote:
> > Hello Pythoneers,
> >
> > I would like to concatenate all members of a list of strings.
> > How can I do that?
> > I know, I can use a for loop over all elements and concatenate
> > them to one string. But is there another more efficient solution?
>
> 1) "sep".join(somelist)
>
> joins all objects in somelist (but the objects must of string-type!)
>
> 2) "sep".join(map(str, somelist))
>
> joins the string-representations of all objects in somelist
>
> regards,
>
> holger
More information about the Python-list
mailing list