On Fri, Mar 13, 2009 at 11:09 AM, ski <nor...@khine.net> wrote: > Hello, > Here is what I have so far: > >>>> mylist = [{'index': 0, 'title': 'Association of British Travel Agents', >>>> 'selected': False, 'edit_row': '?edit_affiliation=0', 'affiliation': >>>> 'ABTA', >>>> 'affiliation_no': u'G3903'}, {'index': 1, 'title': 'Appointed Agents of >>>> IATA', 'selected': False, 'edit_row': '?edit_affiliation=1', 'affiliation': >>>> 'IATA', 'affiliation_no': u'none'}, {'index': 0, 'title': 'Association of >>>> Airline Cons.', 'selected': False, 'edit_row': '?edit_affiliation=0', >>>> 'affiliation': 'AAC', 'affiliation_no': u'sss'}, {'index': 1, 'title': >>>> 'Association of British Travel Agents', 'selected': False, 'edit_row': >>>> '?edit_affiliation=1', 'affiliation': 'ABTA', 'affiliation_no': u'zser'}] > >>>> key = 'affiliation_no' > >>>> my_dup_keys = [] >>>> merged_list = [] > >>>> mykeys = [item['affiliation'] for item in mylist] > >>>> for x in mykeys: > if mykeys.count(x) >= 2: > my_dup_keys.append(x) > >>>> for item in mylist: > if item['affiliation'] in set(my_dup_keys): > merged_list.append(item) > > >>>> values = [d[key] for d in merged_list if d.has_key(key)] >>>> for dict in merged_list: > dict[key] = values > mylist.append(dict) > >>>> print mylist > [{'index': 0, 'title': 'Association of British Travel Agents', 'selected': > False, 'edit_row': '?edit_affiliation=0', 'affiliation': 'ABTA', > 'affiliation_no': [u'G3903', u'zser']}, {'index': 1, 'title': 'Appointed > Agents of IATA', 'selected': False, 'edit_row': '?edit_affiliation=1', > 'affiliation': 'IATA', 'affiliation_no': u'none'}, {'index': 0, 'title': > 'Association of Airline Cons.', 'selected': False, 'edit_row': > '?edit_affiliation=0', 'affiliation': 'AAC', 'affiliation_no': u'sss'}, > {'index': 1, 'title': 'Association of British Travel Agents', 'selected': > False, 'edit_row': '?edit_affiliation=1', 'affiliation': 'ABTA', > 'affiliation_no': [u'G3903', u'zser']}, {'index': 0, 'title': 'Association > of British Travel Agents', 'selected': False, 'edit_row': > '?edit_affiliation=0', 'affiliation': 'ABTA', 'affiliation_no': [u'G3903', > u'zser']}, {'index': 1, 'title': 'Association of British Travel Agents', > 'selected': False, 'edit_row': '?edit_affiliation=1', 'affiliation': 'ABTA', > 'affiliation_no': [u'G3903', u'zser']}] > > > This sort of works but I want to return a list that updates 'mylist' not > append to it, so I will get: > > [{'index': 0, 'title': 'Association of British Travel Agents', 'selected': > False, 'edit_row': '?edit_affiliation=0', 'affiliation': 'ABTA', > 'affiliation_no': [u'G3903', u'zser']}, {'index': 1, 'title': 'Appointed > Agents of IATA', 'selected': False, 'edit_row': '?edit_affiliation=1', > 'affiliation': 'IATA', 'affiliation_no': u'none'}, {'index': 0, 'title': > 'Association of Airline Cons.', 'selected': False, 'edit_row': > '?edit_affiliation=0', 'affiliation': 'AAC', 'affiliation_no': u'sss'}] >
It's a little hard to figure what you're getting at. This looks like a table represented by a list of dictionaries that you'd like to group by "affiliation." affiliation_nos = {} for row in mylist: affiliation_nos.set_default(row['affiliation'],[]).append(row['affliation_no']) will give you a list of affiliation_no's for each affiliation and if you want a list of dicts, you could do mynewlist = [dict(affiliation=affiliation, affiliation_nos = affiliation_nos[affiliation]) for affiliation in affiliation_nos.keys()] I don't know what to do about all the other fields though. In your example input list you have two dictionaries with affiliation = 'ABTA'. In your output you kept the one with index=0 and threw away index=1. (same for 'edit_row') How do you determine which to keep? _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor