joining strings question

Robert Bossy Robert.Bossy at
Fri Feb 29 16:54:57 CET 2008

patrick.waldo at wrote:
> Hi all,
> I have some data with some categories, titles, subtitles, and a link
> to their pdf and I need to join the title and the subtitle for every
> file and divide them into their separate groups.
> So the data comes in like this:
> data = ['RULES', 'title','subtitle','pdf',
> 'title1','subtitle1','pdf1','NOTICES','title2','subtitle2','pdf','title3','subtitle3','pdf']
> What I'd like to see is this:
> [RULES', 'title subtitle','pdf', 'title1 subtitle1','pdf1'],
> ['NOTICES','title2 subtitle2','pdf','title3 subtitle3','pdf'], etc...
> I've racked my brain for a while about this and I can't seem to figure
> it out.  Any ideas would be much appreciated.
As others already said, the data structure is quite unfit. Therefore I 
give you one of the ugliest piece of code I've produced in years:

r = []
for i in xrange(0, len(data), 7):
    r.append([data[i], ' '.join((data[i+1], data[i+2],)), data[i+3], ' 
'.join((data[i+4], data[i+5],)), data[i+6]])
print r


More information about the Python-list mailing list