idiom: concatenate strings with separator

Alex Martelli aleaxit at yahoo.com
Fri May 4 05:29:06 EDT 2001


"Harald Kirsch" <kirschh at lionbioscience.com> wrote in message
news:yv2snilpo82.fsf at lionsp093.lion-ag.de...
    [snip]
> > Note, by the way, that in Python 2 you might also write
> >
> >     return my_favorite_separator.join(map(massage, somethings))
>
> Ah yes, this is definitely the most concise and, to me, the most
> readable version. However two reasons often prevent me from using
> it:
>
> 1) `massage' might need more than one argument. And then it would
> require a lambda with some default arguments set, which tends to be
> less readable.

True!  Either a list comprehension, or a local function,
will tend to be preferable then.

> 2) More important: normally some other things happen also in the loop
> which would be very tedious to stick somehow into the map.

Again true -- and again, a list comprehension (if the "some other
things" are simple enough), or a local function otherwise, may
be a preferable approach.


> Thanks for the info. It is good to know that the streamlined and
> easier to read formulation is in fact even easier on the resources.

You're welcome!  List comprehensions, if I recall correctly from
back when I measured, are roughly resource-neutral wrt explicit
loops, and named local functions wrt lambdas, so the choice can
also be done from a style viewpoint (I'm assuming 10% more or less
matters little, but a factor of 2 [100% or 50% depending on how
you look at it] is more likely to matter, and one of 10 very likely
to BE important in many practical cases).


Alex






More information about the Python-list mailing list