[Tutor] Question on List of Dict

Peter Otten __peter__ at web.de
Fri Sep 19 14:29:47 CEST 2014


Sunil Tech wrote:

> Danny i did it like this
> 
> result_dict = {}
> for i in tes:
>     if i['a'] in result_dict:
>         temp = result_dict[i['a']]
>         temp['b'].append(i['b'])
>         temp['c'].append(i['c'])
>         temp['a'] = i['a']
>         result_dict[i['a']] = temp
>     else:
>         result_dict[i['a']] = {
>             'b': [i['b']],
>             'c': [i['c']],
>             'a': i['a']}
> pprint.pprint(result_dict.values())
> 
> result is
> 
> [{'a': 1, 'b': ['this', 'is', 'sentence'], 'c': [221, 875, 874]},
>  {'a': 2, 'b': ['this', 'another', 'word'], 'c': [215, 754, 745]}]
> 
> any can one improve this method in terms of performance, etc..

What you have is a good solution; the most important part performance-wise 
is that you collect records with the same `a` value in a dict.

For reference here's my two-pass solution to the problem as originally 
specified:

bc = collections.defaultdict(lambda: ([], []))

for rec in tes:
    b, c = bc[rec["a"]]
    b.append(rec["b"])
    c.append(rec["c"])

result = [{"a": a,
           "b": ", ".join(b),
           "c": ", ".join(map(str, c))}
          for a, (b, c) in bc.items()]

If you are flexible with the result data structure you could omit the second 
loop and use bc.items() directly.



More information about the Tutor mailing list