Summing strings is very inefficient; summing strings in a loop requires that a new string is created for each two strings being concatenated, only to be destroyed again when the next string is concatenated with that result.
For example, for summing ['foo', 'bar', 'baz', 'spam', 'ham', 'eggs']
you'd create 'foobar'
, then 'foobarbaz'
, then 'foobarbazspam'
, then 'foobarbazspamham'
, then finally 'foobarbazspamhameggs'
, discarding all but the last string object.
You'd use the str.join()
method instead:
''.join(str_list)
which creates one new string and copies in the contents of the constituent strings.
Note that sum()
uses a default starting value of 0
, which is why you get your specific exception message:
>>> 0 + ''
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'str'
You can give sum()
a different starting value as the second argument; for strings that'll give you a more meaningful error message:
>>> sum(['foo', 'bar'], '')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: sum() can't sum strings [use ''.join(seq) instead]
The function is otherwise not limited to just numbers; you can use it for any other type that defines __add__
operations, but you have to specify a sensible start value. You could 'sum' lists, for example:
>>> sum([['foo', 'bar'], ['ham', 'spam']], [])
['foo', 'bar', 'ham', 'spam']
but note the []
value for the second (start
) argument! This is also just as inefficient as summing strings; the efficient method would be using list(itertools.chain.from_iterable(list_of_lists))
.