Steven
Steven

Reputation: 879

Algorithm to add new, update existing and remove missing data

I have done lots of searching, but been unable to find a satisfactory answer to the most efficient approach to achieve the following.

Say my App contains a list of Products. At the end of every day an external service is called that returns another list of Products from a master data source.

At the moment, I do a loop on each list, looping through the other list for each Product:

I'm wondering if there is a more efficient method to achieve this? Or any algorithms or patterns that are relevant here?

In each case the Products are represented by objects in a Python list.

Upvotes: 0

Views: 125

Answers (1)

gimix
gimix

Reputation: 3833

First of all I'd suggest to use dicts with the Product code (or name or whatever) as key and the Product object as value. This should make your loops faster by at least a 100x factor on a thousand entries.

Then especially for the second search it may be worth exploring the possibility of converting the keys of the first dict to a set and looping on the difference as in

for i in set(appDict.keys()).difference(masterDict.keys()):
    ##update unavailable Product data

Upvotes: 1

Related Questions