idiom: concatenate strings with separator

Harald Kirsch kirschh at lionbioscience.com
Thu May 3 06:38:56 EDT 2001


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.

  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