How to concatenate list members

Peter Hansen peter at engcorp.com
Thu May 30 19:27:06 EDT 2002


Christopher wrote:
> 
> 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.

This would probably be more readable as:

def cs(*args):
    return ''.join(map(str,args))

Changing the name from "cs" to "joinAsStrings" or something 
would also improve the readability and maintainability.

-Peter



More information about the Python-list mailing list