transforming a list into a string

Jp Calderone exarkun at divmod.com
Sat Jul 31 18:29:38 CEST 2004


> In article <pan.2004.07.31.12.27.23.547000 at hotmail.com>,
> [snip]
> 
> This works, and is pretty straight-forward:
> 
> source = ['1','2','7','8','12','13']
> temp = []
> 
> while source:
>     x = source.pop(0)
>     y = source.pop(0)
>     temp.append ('{%s,%s}' % (x, y))
> 
> result = ','.join (temp)
> print result
> 
> [snip]
> 
> How long is "much longer", and how important is it that this runs fast?  
> The code above runs in O(n).  You can probably play some tricks to tweak 
> the speed a little, but in the big picture, you're not going to do any 
> better than O(n).
> 

   Are you sure?  Did you consider the complexity of list.pop(0)?

   Jp



More information about the Python-list mailing list