python - Find dicts from lists the have the closest value in a faster/better way -
i tried searching response not find 1 though i'm sure must have been asked before. must not searching correct phrases. my problem have 2 large lists of dicts , trying match dicts in list dict in list b has closest value particular key, in case timestamp . timestamps dicts may or may not same, , want proceed acting on dict list if has match list b timestamp value within 15 of it's timestamp. dictionaries not identical in structure both contain timestamp key value pair. first tried similar this: for itema in lista: closestitemb = min(listb, key=lambda x :abs(x["timestamp"])-int(itema["timestamp")) if(abs(itema['timestamp'] - closestitemb['timestamp']) < 15: #write info both dicts csv file this extremely slow large lists. realized lists both ordered timestamp should possible speed significantly my thought process on first loops through search entire list b closest , next time through search through small slice b