'Fastest alternative to dictionary of lists in Python
I have a dictionary of lists having over 600 keys (strings), and a list of 1440 elements for each key (floats). From each of the lists I get a minimum to compare with some values. Because it is done very often I was wondering if there is a faster and more efficient way to do it. Maybe with pandas or numpy? I am not familiar with them unfortunately.
I create the empty dictionary with:
dict = {el:[] for el in symbols}
Symbols is a list of 610 string values.
Then every 60 seconds I append one values to every key. The maximum number of elements is 1440.
dict[symbol].append(value)
dict[symbol] = dict[symbol][-1440:]
Next I get the minimum and compare it.
if to_be_compared_for_symbol < min(dict[symbol]):
...
This is repeated over and over. Every 60 seconds new values are put in to the dictionary, but the comparison is made about 5 times a second. To be more precise: I get 610 different values about five times a second and every one of them has to be compared with their corresponding minimum.
Can it be done more efficient and faster?
Solution 1:[1]
You are trying to extract minimums of your lists 5 times a second, but you get new values for your lists every 60 seconds (repeating what you said in order to be on the same page).
I'd just find 610 minimums for all of your lists in the dict and save them in a plain list, then 5 times a second compare with them, instead searching for mins all over again. Then, after you append to your dict lists, find new minimums, store them and repeat.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | FlyingPlushie |
