Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the simplest way to merge a list of dictionaries with same value?

Tags:

python

I would like to know is there a simple and efficient way to format this formal dictionary to the result as shown? Basically, the time key is used to group the dictionary and the value of variableName is used as key with value key as value.

For example here is the list of dictionary:

testdata =[{
  'frequency': 'monthly',
  'level': '1',
  'time': '2017 Jan',
  'value': 99.524,
  'variableCode': 'M212191.1',
  'variableName': 'All Items'},
 {'frequency': 'monthly',
  'level': '2',
  'time': '2017 Jan',
  'value': 105.12,
  'variableCode': 'M212191.1.0',
  'variableName': 'Food'},
 {'frequency': 'monthly',
  'level': '1',
  'time': '2017 Feb',
  'value': 99.521,
  'variableCode': 'M212191.1',
  'variableName': 'All Items'},
 {'frequency': 'monthly',
  'level': '2',
  'time': '2017 Feb',
  'value': 105.078,
  'variableCode': 'M212191.1.0',
  'variableName': 'Food'},
]

But I would like it to have the result in this following format:

testdata = [
   {
       'time': '2017 Jan',
       'All Items': 99.524,
       'Food':105.12
   },    
   {
       'time': '2017 Feb',
       'All Items': 99.521,
       'Food':105.078
   },
]

So far here is my progress which I am stuck with..

import itertools
import operator
import pprint

result = sorted(testdata, key = lambda i: i['time'])
list1 = []

for key, items in itertools.groupby(result, operator.itemgetter('time')):
   list1.append(list(items))

pprint.pprint(list1)

Output:

[[{'frequency': 'monthly',
   'level': '1',
   'time': '2017 Feb',
   'value': 99.521,
   'variableCode': 'M212191.1',
   'variableName': 'All Items'},
  {'frequency': 'monthly',
   'level': '2',
   'time': '2017 Feb',
   'value': 105.078,
   'variableCode': 'M212191.1.0',
   'variableName': 'Food'}],
 [{'frequency': 'monthly',
   'level': '1',
   'time': '2017 Jan',
   'value': 99.524,
   'variableCode': 'M212191.1',
   'variableName': 'All Items'},
  {'frequency': 'monthly',
   'level': '2',
   'time': '2017 Jan',
   'value': 105.12,
   'variableCode': 'M212191.1.0',
   'variableName': 'Food'}]]

like image 946
Shurya Ang Avatar asked Jan 21 '26 12:01

Shurya Ang


1 Answers

I think you are close enough - just loop through the items once more.

from itertools import groupby
from operator import itemgetter

d = []

for k, grp in groupby(testdata,key=itemgetter("time")):
    temp = {"time":k}
    for i in grp:
        temp[i.get("variableName")] = i.get("value")
    d.append(temp)

print (d)

Result:

[{'time': '2017 Jan', 'All Items': 99.524, 'Food': 105.12}, {'time': '2017 Feb', 'All Items': 99.521, 'Food': 105.078}]
like image 87
Henry Yik Avatar answered Jan 24 '26 01:01

Henry Yik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!