Python has a built in function sum
, which is effectively equivalent to:
def sum(iterable, start):
return start + reduce(operator.add, iterable)
for all types of parameters except strings. It works for numbers and lists, for example.
Why were strings specially left out?
I seem to remember discussions in the Python list for the reason, so an explanation or a link to a thread explaining it would be fine.
Edit: I am aware that the standard way is to do "".join
. My question is why the option of using sum for strings was banned, and no banning was there for, say, lists.
Edit 2: Although I believe this is not needed given all the good answers I got, the question is: Why does sum work on an iterable containing numbers or an iterable containing lists but not an iterable containing strings?