0

I have one of list of dictionaries which I need to compare against another list. I need to only consider items which are present in the 2nd list from list of dictionaries.

In list of dictionaries there can be multiple values against a value in list.

It works fine if both list contains less items. But it takes too long to finish if lists are large. Say my list of dictionaries (myDict) have 500k items, and my 2nd list (filterDict) contains 400k items then process takes too long to complete (it is running after 10 mins) .

myDict = [{'project': "mobileapp1", 'id': "011", 'start_date': '2021-01-01'},
               {'project': "webapp", 'id': "012", 'start_date': '2021-01-01'},
               {'project': "mobileapp2", 'id': "013", 'start_date': '2021-10-01'},
               {'project': "mobileapp2", 'id': "011", 'start_date': '2021-09-01'}]
    
filterDict = ["011",'013']
    
#1 soltion
my_new_dict = [item for item in myDict  if item['id'] in filterDict]
#2 solution
temp = list(filter(lambda d: d['id'] in filterDict, myDict))
print(temp)
print(my_new_dict)

[{'project': 'mobileapp1', 'id': '011', 'start_date': '2021-01-01'}, {'project': 'mobileapp2', 'id': '013', 'start_date': '2021-10-01'}, {'project': 'mobileapp2', 'id': '011', 'start_date': '2021-09-01'}]

Any help would be greatly appreciated.

user2026504
  • 69
  • 1
  • 9

0 Answers0