I have currently a dictionary with nested dictionaries. Its length is approx 2 millions. The dictionary looks like this, but this is a fake example
{ "item 1" : { "name" : "John Doe", "address" : "#1 city, zip, whatever"},
"item 2" : { "name" : "Jane Doe", "address" : "#2 city, zip, blablabla"},
...}
My task is to get the first n items where the "address" field in the nested dict contains a string, where n let's say 10. This must be very efficient, responding in ms on a powerful desktop. Tried loop with iterators with exception handling, but was too slow. Dict comprehension iterates over every elements, so also slow. Then I created an index dictionary where the key was the address and the value was a list of items (key of the original dict). Then iterated through and stopped after n items. Something like this (dict_2):
{"#1 city, zip, whatever" : ["item 1", "item 5487", ...],
"#2 city, zip, whatever" : ["item 2", "item 1654654", ...] }
result = []
i = 0
for k,v in dict_2.items():
if findThis in k:
i += 1
result.extend(v)
if i>= n:
break
Quite ok, but I still need some improvement, because python loops are not as fast as I need. Compehension does not break after n match.
I can accept any kind of solutions (series, list, dict, hashmap, etc.), but the goal is: response time as low as possible; result is a list of keys of the original dictionary.
Thank you in advance!