You are the man, you are the man!! Yes, i need those dicts in order to assign them to a nested <TMPL_LOOP> for the htmltmpl templating engine. Thats why i cant use the solution provided by Max, and thanks to Jon too!! Thanks a lot dudes, i hope someday ill turn myself into some of you guys who can actually answer questions ;)
Gerardo >> result = [{'service_id' : 1, 'value': 10}, >> {'service_id': 2, 'value': 5}, >> {'service_id': 1, 'value': 15}, >> {'service_id': 2, 'value': 15}, >> ] >> >> and so on...what i need to do is some list comprehension that returns >> me something like >> >> result = [ >> { >> 'service_id' : 1, 'values': [ {'value': 10}, >> {'value': 15}] >> }, >> { >> 'service_id' : 2, 'values': [ {'value': 5}, {'value': 15}] >> } >> >> My problem now is i cant avoid have "repeteated" entries, lets say, >> in this particular case, 2 entries for "service_id = 1", and other 2 >> for "service_id =2". > > > Okay...while I'm not sure the opacity of a one-liner is actually > productive, it *can* be done. Whether it should, I leave that to your > discernment. :) > > >>> [{'service_id': i, 'values':[{'value':d2['value']} for d2 in > result if d2['service_id'] == i ]} for i in set(d['service_id'] for d > in result)] > > [{'service_id': 1, 'values': [{'value': 10}, {'value': 15}]}, > {'service_id': 2, 'values': [{'value': 5}, {'value': 15}]}] > > > There's no claiming it's efficient, as it looks like there may be some > O(N^2) logic going on under the hood (or possibly O(N*M) where N is > the size of the result-set and M is the count of unique service_id > values), as it's iterating over the result-set in two > dimensions...once to create the set of top-level indices, and once for > each result. > > If you didn't have to have all those dictionaries around, it might > come out more cleanly to just have some result-set that came out to be > > {1: [10,15], 2: [5,15]} > > Just a few thoughts... > > -tkc > > > > -- http://mail.python.org/mailman/listinfo/python-list