idiom: concatenate strings with separator

Harald Kirsch kirschh at lionbioscience.com
Fri May 4 03:28:29 EDT 2001


"Alex Martelli" <aleaxit at yahoo.com> writes:

> "Harald Kirsch" <kirschh at lionbioscience.com> wrote in message
> news:yv2r8y6bttr.fsf at lionsp093.lion-ag.de...
> >
> > Recently I started using code like this
> >
> > l = []
> > for x in somethings:
> >   y = massage(x)
> >   l.append(y)
> >
> > return string.join(y, my_favorite_separator)
> 
> You presumably mean l, not y, here?

Sure.

> 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.

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

But you actually said that already:

> So, if you do have any special interest in getting
> better performance: switching to map, where feasible,
> rather than an explicit loop, MAY be worth it, as it
> may be able to reduce your overhead by a factor of
> 2 or 3; using join IS definitely worthwhile, as it
> speeds things up by dozen of times even on rather
> small cases (and by more on larger ones, I believe).

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.

  Harald Kirsch

-- 
----------------+------------------------------------------------------
Harald Kirsch   | kirschh at lionbioscience.com | "How old is the epsilon?"
LION bioscience | +49 6221 4038 172          |        -- Paul Erdös
       *** Please do not send me copies of your posts. ***



More information about the Python-list mailing list