Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Group dictionary key values in python

I have the following dictionary

mylist = [{'tpdt': '0.00', 'tmst': 45.0, 'tmdt': 45.0, 'pbc': 30, 'remarks': False, 'shift': 1, 'ebct': '0.00', 'tmcdt': '0.00', 'mc_no': 'KA20'}, 
          {'tpdt': '0.00', 'tmst': 45.0, 'tmdt': 45.0, 'pbc': 30, 'remarks': False, 'shift': 1, 'ebct': '0.00', 'tmcdt': '0.00', 'mc_no': 'KA20'}, 
          {'tpdt': '0.00', 'tmst': 55.0, 'tmdt': 55.0, 'pbc': 30, 'remarks': False, 'shift': 1, 'ebct': '0.00', 'tmcdt': '0.00', 'mc_no': 'KA23'}, 
          {'tpdt': '0.00', 'tmst': 55.0, 'tmdt': 55.0, 'pbc': 30, 'remarks': False, 'shift': 1, 'ebct': '0.00', 'tmcdt': '0.00', 'mc_no': 'KA23'}]

I want to get the sum of the key 'tmst' for every dictionary values 'KA20' and 'KA23' in the list of dictionaries.

Could you please have your suggestions on this??

like image 754
Alchemist777 Avatar asked Aug 12 '13 05:08

Alchemist777


People also ask

How do you group dictionary using keys?

Method : Using sorted() + items() + defaultdict() The defaultdict() is used to create a dictionary initialized with lists, items() gets the key-value pair and grouping is helped by sorted().

Can you assign multiple values to a dictionary key in Python?

In python, if we want a dictionary in which one key has multiple values, then we need to associate an object with each key as value. This value object should be capable of having various values inside it. We can either use a tuple or a list as a value in the dictionary to associate multiple values with a key.

Can you have 3 items in a dictionary Python?

You can add as many items as you like.

What does .values mean in Python?

values() is an inbuilt method in Python programming language that returns a view object. The view object contains the values of the dictionary, as a list.


2 Answers

You can use itertools.groupby:

>>> for key, group in itertools.groupby(mylist, lambda item: item["mc_no"]):
...     print key, sum([item["tmst"] for item in group])
... 
KA20 90.0
KA23 110.0

Note that for groupby to work properly, mylist has to be sorted by the grouping key:

from operator import itemgetter

mylist.sort(key=itemgetter("mc_no"))
like image 199
alecxe Avatar answered Oct 14 '22 10:10

alecxe


First you needs to sort that list of dictionaries..using following code.. Ex.

animals = [{'name':'cow', 'size':'large'},{'name':'bird', 'size':'small'},{'name':'fish', 'size':'small'},{'name':'rabbit', 'size':'medium'},{'name':'pony', 'size':'large'},{'name':'squirrel', 'size':'medium'},{'name':'fox', 'size':'medium'}]

import itertools
from operator import itemgetter
sorted_animals = sorted(animals, key=itemgetter('size'))

then you use following code

for key, group in itertools.groupby(sorted_animals, key=lambda x:x['size']):
    print key,
    print list(group)
After you will get following result...
large [{'name': 'cow', 'size': 'large'}, {'name': 'pony', 'size':'large'}]
medium [{'name': 'rabbit', 'size': 'medium'}, {'name': 'squirrel', 'size':'medium'}, {'name': 'fox', 'size': 'medium'}]
small [{'name': 'bird', 'size': 'small'}, {'name': 'fish', 'size': 'small'}]
like image 27
Milan Thakkar Avatar answered Oct 14 '22 10:10

Milan Thakkar