transforming a list into a string

Tom B. sbabbitt at commspeed.net
Mon Aug 2 20:24:32 EDT 2004


"jblazi" <jblazi at hotmail.com> wrote in message
news:pan.2004.07.31.12.27.23.547000 at hotmail.com...
> 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.)
>
> TIA,
>
> jb
>
Try,'
>>>items = ['1','2','7','8','12','13']
>>>import string
>>>string.join([('{'+str(items[x])+','+str(items[x+1])+'}') for x in
range(0,len(items)-1,2)])

Tom





More information about the Python-list mailing list