transforming a list into a string

Peter Otten __peter__ at web.de
Sat Jul 31 08:44:17 EDT 2004


jblazi wrote:

> Let us assume I have a list like
> 
> ['1','2','7','8','12','13]
> 
> and would like to transoform it into the string
> 
> '{1,2},{7,8},{12,13}'
> 
> Which is the simplest way of achiebing this? (The list is in fact much
> longer and I may have to cut the resulting strings into chunks of 100 or
> so.)

>>> from itertools import izip
>>> items = ['1','2','7','8','12','13']
>>> it = iter(items)
>>> ",".join(["{%s,%s}" % i for i in izip(it, it)])
'{1,2},{7,8},{12,13}'


Peter




More information about the Python-list mailing list