Great answer from SilenGhost BUT, just a few words about the presented reduce
"alternative"
Unless you've got a very very VERY good reason to concatenate strings using +
or operator.add
(the most frequent one, that you've got few, fixed number of strings), you should use always join
.
Just because each +
generates a new string which is the concatenation of two strings, unless join that only generates one final string. So, imagine you've got 3 strings:
A + B + C
-->
D = A + B
final = D + C
Ok, doesn't seems not much, but you've got to reserve memory for D. Also, due python use of strings, generating a new, intermediate, string, it's somehow expensive...
Now, with 5 strings
A + B + C + D + E
-->
F = A + B
G = F + C
H = G + D
final = H + E
Assuming the best scenario (if we do (A+B) + (C+D) + E, we'll end having three intermediate strings at the same time on memory), that's generating 3 intermediate strings... You've got to generate a new python object, reserve memory space, release the memory a few times... Also the overhead of calling a Python function (that is not small)
Now think of it with 200 strings. We'll end up with a ridiculous big number of intermediate strings, each of one consuming combining quite a lot time on being a complete list over python, and calling a lot of operator.add
functions, each with its overhead... Even if you use reduce
functions, it won't help. It's a problem that has to be managed with a different approach: join
, which only generates ONE complete python string, the final one and calls ONE python function.
(Of course, join
, or other similar, specialized function for arrays)