14

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 "-".

cs95
  • 379,657
  • 97
  • 704
  • 746
Mahamutha M
  • 1,235
  • 1
  • 8
  • 24

4 Answers4

13

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         -
Mayank Porwal
  • 33,470
  • 8
  • 37
  • 58
9

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}]
cs95
  • 379,657
  • 97
  • 704
  • 746
2

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}]
timgeb
  • 76,762
  • 20
  • 123
  • 145
2

With a more verbose for loop, updates objects of list2:

for item2 in list2:
  item2['att_value'] = '-'
  item1 = [ item1 for item1 in list1 if item1['count'] == item2['count'] ]
  if item1: item2.update(item1[0])

print(list2)
#=> [{'count': 359, 'person_id': 4, 'att_value': 'nine'}, {'count': 351, 'person_id': 12, 'att_value': 'one'}, {'count': 381, 'person_id': 8, 'att_value': '-'}]
iGian
  • 11,023
  • 3
  • 21
  • 36