[Tutor] Converting string or list to sum

Kalle Svensson kalle at lysator.liu.se
Fri Jan 30 12:36:32 EST 2004


[Daniel Ehrenberg]
> 
> > >  I think I can put two ideas together to solve it
> > for lists :
> > >  >>> a=[2, 3, '+', 4]
> > >  >>> d=""
> > >  >>> for item in range(len(a)):
> > >  ...     d=d+str(a[item])
> > >  ...
> > 
> > Or even easier:
> > 
> > for item in a: 
> >     d = d + str(item)
> > 
> > No need for indexing etc,
> > 
> > Alan G.
> 
> In that case, is there any reason not to just do
> 
> for item in a:
>     d += str(item)

I'd write

  d = ''.join(map(str, a))

since that's probably much faster for large lists.

Peace,
  Kalle
-- 
Kalle Svensson, http://www.juckapan.org/~kalle/
Student, root and saint in the Church of Emacs.



More information about the Tutor mailing list