[EMAIL PROTECTED] 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. > > Thanks
data = ['RULES', 'title','subtitle','pdf', 'title1','subtitle1','pdf1','NOTICES','title2','subtitle2','pdf','title3','subtitle3','pdf'] olist = [] while data: if data[0] == data[0].upper(): olist.append([data[0]]) del data[0] else: olist[-1].append(data[0]+' '+data[1]) olist[-1].append(data[2]) del data[:3] print olist However, I suspect you should be asking yourself whether this is really an appropriate data structure for your needs. If you say what you are trying to achieve in the large rather than focusing on a limited programming issue there may be much better solutions. I suspect, for example, that a dict indexed by the categories and with the entries each containing a list of tuples might suit your needs much better, i.e. { 'RULES': [('title subtitle', 'pdf'), ('title1 subtitle1', 'pdf')], 'NOTICES': [('title2 subtitle2', 'pdf'), 'title3 subtitle3', 'pdf')]} One final observation: if all the files are PDFs then you might just as well throw the 'pdf' strings away and use a constant extension when you try and open them or whatever :-). Then the lists of tuples i the dict example could just become lists of strings. regards Steve -- Steve Holden +1 571 484 6266 +1 800 494 3119 Holden Web LLC http://www.holdenweb.com/ -- http://mail.python.org/mailman/listinfo/python-list