Search code examples
pythonaggregate

List of dictionaries collection based on total and groupby of keys


My dictionary looks like following

d = [{'status': 'Red', 'name': 'Alex'}, {'status': 'Red', 'name': 'Alex'}, 
     {'status': 'Green', 'name': 'Alex'}, {'status': 'Yellow', 'name': 'Peter'}, 
     {'status': 'Green', 'name': 'Mike'}, {'status': 'Yellow', 'name': 'Alex'}, 
     {'status': 'Green', 'name': 'Peter'}, {'status': 'Red', 'name': 'Mike'}, 
     {'status': 'Yellow', 'name': 'Alex'}]

I am trying to aggregate it in most efficient way, my desired output should look like following

d = [{"name": "Alex", "Red": 2, "Green": 1, "Yellow": 2, "Total": 5},
     {"name": "Peter", "Red": 0, "Green": 1, "Yellow": 1, "Total": 2},
     {"name": "Mike", "Red": 1, "Green": 1, "Yellow": 0, "Total": 2}
    ]

I am able to aggregate by total count but having trouble grouping it by 'status' value

from collections import Counter
output = Counter(i['name'] for i in d)

Solution

  • You can do

    from collections import defaultdict
    
    res = defaultdict(list)
    unique = set()
    for i in d:
        res[i['name']].append(i['status'])
        unique.add(i['status'])
    res_total = [{'name': k, **Counter(v), **{ i: 0 for i in unique - set(v)}} for k, v in res.items()]
    print(res_total)
    

    Output

    [{'name': 'Alex', 'Red': 2, 'Green': 1, 'Yellow': 2},
     {'name': 'Peter', 'Yellow': 1, 'Green': 1, 'Red': 0},
     {'name': 'Mike', 'Green': 1, 'Red': 1, 'Yellow': 0}]