flatten a level one list

Nick Craig-Wood nick at craig-wood.com
Fri Jan 13 07:30:03 EST 2006


Alex Martelli <aleax at mail.comcast.net> wrote:
>  Nick Craig-Wood <nick at craig-wood.com> wrote:
> > 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.

Hmm, interesting...

I agree with Guido about the special case, but I disagree about the
error message.  Not being able to use sum(L,"") reduces the
orthogonality of sum for no good reason I could see.

However I only noticed that when trying to do the recent python
challenge which probaby isn't the best use case ;-)

-- 
Nick Craig-Wood <nick at craig-wood.com> -- http://www.craig-wood.com/nick



More information about the Python-list mailing list