idiom: concatenate strings with separator

Remco Gerlich scarblac at pino.selwerd.nl
Thu May 3 07:18:03 EDT 2001


Harald Kirsch <kirschh at lionbioscience.com> wrote in comp.lang.python:
> 
> 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)
> 
> 
> to produce strings made of substrings separated by something. Note
> that with immediate string concatenation its hard to avoid the
> separator either in front or at the end of the produced string.
> 
> Nevertheless I wonder if something like
> 
> s = ""
> sep = ""
> for x in somethings:
>   y = massage(x)
>   s = s + sep + y
>   sep = my_favorite_separator
> 
> return s
> 
> is faster or uses less memory than the previous solution.

What happened when you tried it out in the profiler? :)

At first sight, I'd say that the second must be slower because of all the
string copying every time s is assigned to. However, the profiler showed
this was not the case... I use range(20) for somethings, the massage
function was str(), and "," for a seperator. 20,000 iterations. The first
method took about 4.8 seconds, second about 4.2.

Luckily, the 2.0 idiom I was going to suggest,

seperator.join([massage(x) for x in somethings])

took only 3.4 seconds. Since map() is often faster than a simple list
comprehension, I tried that too, and

seperator.join(map(massage, somethings))

takes only 2.5 seconds. And it's the shortest so far, too. And spelled as

string.join(map(massage, somethings), seperator)

It works on 1.5.2 as well. So if you want speed, use that :).

-- 
Remco Gerlich



More information about the Python-list mailing list