How to find an intersection of two list of dictionaries based on a key?

  • A+

I have two different list of dictionaries,

list1 = [{'count': 351, 'att_value': 'one'},          {'count': 332,  'att_value': 'two'},          {'count': 336,  'att_value': 'six'},          {'count': 359,  'att_value': 'nine'},          {'count': 304,  'att_value': 'four'}]  list2 = [{'count': 359,'person_id' : 4},          {'count': 351, 'person_id' : 12},          {'count': 381, 'person_id' : 8}] 

How to find an intersection of list_A with list_B based on "count" key by including rest of the key as like list_C?

list3 = [{'count':359, 'att_value' : 'nine', 'person_id':4},          {'count':351, 'att_value' : 'one', 'person_id':12},          {'count':381, 'att_value' : '-', 'person_id':8}]  

I would like to retain the keys from list2, but with missing values from list1 represented by "-".


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 = set(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}] 


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: