Intersection of two list of dictionaries based on a key
You can also use pandas library for this:
In [102]: df1 = pd.DataFrame(list1)
In [104]: df2 = pd.DataFrame(list2)
In [106]: pd.merge(df2,df1, on='count', how='left').fillna('-')
Out[106]:
count att_value
0 359 nine
1 351 one
2 381 -
Assuming the dictionaries in list1
share the same keys and that you have Python 3.5 or newer, you can write the following list comprehension.
>>> count2dict = {d['count']:d for d in list1}
>>> dummies = dict.fromkeys(list1[0], '-')
>>> [{**count2dict.get(d['count'], dummies), **d} for d in list2]
[{'count': 359, 'att_value': 'nine', 'person_id': 4},
{'count': 351, 'att_value': 'one', 'person_id': 12},
{'count': 381, 'att_value': '-', 'person_id': 8}]
You can do this with a list comprehension. First, build a set of all counts from list2
, and then filter out dictionaries based on constant time set membership check.
counts = {d2['count'] for d2 in list2}
list3 = [d for d in list1 if d['count'] in counts]
print(list3)
# [{'count': 351, 'att_value': 'one', 'person_id': 12},
# {'count': 359, 'att_value': 'nine', 'person_id': 4}]
(Re:Edit) To handle other keys (besides just "att_value") appropriately, giving a default value of '-' in the same way, you can use:
keys = list1[0].keys() - {'count'}
idx = {d['count'] : d for d in list1}
list3 = []
for d in list2:
d2 = idx.get(d['count'], dict.fromkeys(keys, '-'))
d2.update(d)
list3.append(d2)
print(list3)
# [{'count': 359, 'att_value': 'nine', 'person_id': 4},
# {'count': 351, 'att_value': 'one', 'person_id': 12},
# {'person_id': 8, 'att_value': '-', 'count': 381}]