Re: [Tutor] Converting string or list to sum

Isr Gish isrgish at fusemail.com
Fri Jan 30 17:26:22 EST 2004



Kalle Svensson Wrote:
     >[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.
   >
 
I think that should be:
import __builtin__
d = ''.join(map(__builtin__.str, a))

Good luck
Isr




More information about the Tutor mailing list