Merge Two List of Dict

Peter Otten __peter__ at web.de
Thu Dec 1 03:32:39 EST 2016


Nikhil Verma wrote:

> Hey guys
> 
> What is the most optimal and pythonic solution forthis situation
> 
> A = [{'person_id': '1', 'adop_count': '2'}, {'person_id': '3',
> 'adop_count': '4'}]
> *len(A) might be above 10L*
> 
> B = [{'person_id': '1', 'village_id': '3'}, {'person_id': '3',
> 'village_id': '4'}]
> *len(B) might be above 20L*
> 
> 
> OutPut List should be
> 
> C = B = [{'adop_count': '2', 'village_id': '3'}, {'adop_count': '4',
> 'village_id': '4'}]
> 
> Thanks in advance

Build a lookup table that maps person_id to village_id:

>>> A = [{'person_id': '1', 'adop_count': '2'}, {'person_id': '3',
... 'adop_count': '4'}]
>>> B = [{'person_id': '1', 'village_id': '3'}, {'person_id': '3',
... 'village_id': '4'}]
>>> p2v = {item["person_id"]: item["village_id"] for item in B}
>>> assert len(B) == len(p2v), "duplicate person_id"
>>> import collections
>>> v2a = collections.defaultdict(int)
>>> for item in A:
...     v2a[p2v[item["person_id"]]] += int(item["adop_count"])
... 
>>> [{"adop_count": str(v), "village_id": k} for k, v in v2a.items()]
[{'adop_count': '4', 'village_id': '4'}, {'adop_count': '2', 'village_id': 
'3'}]

If the data stems from a database you can run (untested)

select B.village_id, sum(A.adop_count) from A inner join B on A.person_id = 
B.person_id;



More information about the Python-list mailing list