flatten a level one list

Alex Martelli aleax at mail.comcast.net
Fri Jan 13 07:45:29 CET 2006


Nick Craig-Wood <nick at craig-wood.com> wrote:

> Sion Arrowsmith <siona at chiark.greenend.org.uk> wrote:
> >  sum(...)
> >      sum(sequence, start=0) -> value
> > 
> >  If you're using sum() as a 1-level flatten you need to give it
> >  start=[].
> 
> Except if you are trying to sum arrays of strings...
> 
>   >>> sum(["a","b","c"], "")
>   Traceback (most recent call last):
>     File "<stdin>", line 1, in ?
>   TypeError: sum() can't sum strings [use ''.join(seq) instead]
>   >>> 
> 
> I've no idea why this limitation is here... perhaps it is because pre
> python2.4 calling += on strings was very slow?

No: when I implemented sum, I originally specialcased sum on strings to
map down to a ''.join -- Guido decided it was confusing and had no
advantage wrt calling ''.join directly so he made me put in that check.


Alex



More information about the Python-list mailing list