[Tutor] update list of dictionaries based on key

ski norman at khine.net
Fri Mar 13 17:23:32 CET 2009



greg whittier wrote:
> On Fri, Mar 13, 2009 at 11:09 AM, ski <norman at 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."

i have objects in my database, called addresses, each address has a 
metadata called affiliation.

also each address is a member of a company.

in my company class, i wanted to get all the addresses affiliations and 
only list the unique values.


> 
> 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?

I want to keep it based on the 'affiliation' key.


In essence, from my original 'mylist' I want to keep all the list items 
as they are, but just update the list items that have more than one 
'affiliation' by updating only the 'affiliation_no' to contain both entries.

Perhaps there is a simpler way to do this ;)

I will change my code so that mylist does not contain the 'index' and 
'edit_row' keys. Perhaps then it will be easier to update the dictionary?




> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 



More information about the Tutor mailing list