Sunil Garg Sunil Garg - 2 years ago 78
Python Question

python group by data based on one field of list

I have input data like this

input = ((1,'MCA', 'Science'),(2,'physic', 'Science'),(3,'chemsitry', 'Science'),(4,'punjabi', 'arts'),(5,'hindi', 'arts'))

I want to group this data by third field (Science/arts) like this

result = {"arts":[{"id":"4","name":"punjabi"},{"id":"5","name":"hindi"}],"Science":[{"id":"1","name":"MCA"},{"id":"2","name":"physics"},{"id":"3","name":"chemistry"}]}

how can i achieve this in efficent way ?

Answer Source

I would recommend a collections.defaultdict. Iterate over your original data and assign new dictionaries to this defaultdict.

import collections
result = collections.defaultdict(list)
i = ((1,'MCA', 'Science'),(2,'physic', 'Science'),(3,'chemsitry', 'Science'),(4,'punjabi', 'arts'),(5,'hindi', 'arts'))
for id,name,subject in i:
    result[subject].append({'id':str(id), 'name':name})

You end up with the following result:

>>> result
defaultdict(<class 'list'>, {'Science': [{'name': 'MCA', 'id': '1'}, {'name': 'physic', 'id': '2'}, {'name': 'chemsitry', 'id': '3'}], 'arts': [{'name': 'punjabi', 'id': '4'}, {'name': 'hindi', 'id': '5'}]})

It doesn't affect the algorithm, but remember to double-check the content's spelling before putting it into the program (viz 'chemsitry' et al.).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download