This post will walk through an example of why defining a known constant can save lots of computational time.
How to find the key with the maximum value in a Python dictionary
There’s a few ways to go about getting the key associated with the max value in a Python dictionary. The two ways we’ll show each involve using a list comprehension.
First, let’s set the scene by creating a dictionary with 100,000 key-value pairs. We’ll just make the keys the integers between 0 and 99,999 and we’ll use the random package to randomly assign values for each of these keys based off the uniform distribution between 0 and 100,000.
import random import time vals = [random.uniform(0, 100000) for x in range(100000)] mapping = dict(zip(range(100000), vals))
Now, mapping contains 100,000 key-value pairs.
The first approach we’ll try is using a list comprehension to loop over mapping.items(). This list comprehension contains an if statement checking each particular value for whether it is equal to the max of the values in the mapping dict. As a reminder, using the items method on a dictionary will return a collection of the key-value tuples comprising the key-value mapping seen in the dictionary (this collection has what’s known as the dict_items data type).
Note – in this approach we’re calculating the max of mapping.values() in every iteration of the list comprehension. While calculating this once doesn’t appear to take much time, it actually adds up to quite a bit of time the larger our list comprehension becomes.
start = time.time() max_key = [key for key,val in mapping.items() if val == max(mapping.values())] end = time.time() print(end - start)
As we can see, this approach took over 169 seconds, which is…very slow. Let’s see the effect of defining a constant for max(mapping.values()).
Much more efficient approach
After we run the slow-running code above, let’s do things more efficiently. Since the max across the values of mapping doesn’t change, we just need to calculate it once and store that value as a variable.
start = time.time() m = max(mapping.values()) # define constant for max across values max_key = [key for key,val in mapping.items() if val == m] end = time.time() print(end - start)
In this case, our task is done in a fraction of a second! This is much faster than our initial approach. As we increase the size of the dictionary, this time difference becomes more and more pronounced.
Originally posted on TheAutomatic.net blog.
Disclosure: Interactive Brokers
Information posted on IBKR Campus that is provided by third-parties and not by Interactive Brokers does NOT constitute a recommendation by Interactive Brokers that you should contract for the services of that third party. Third-party participants who contribute to IBKR Campus are independent of Interactive Brokers and Interactive Brokers does not make any representations or warranties concerning the services offered, their past or future performance, or the accuracy of the information provided by the third party. Past performance is no guarantee of future results.
This material is from TheAutomatic.net and is being posted with permission from TheAutomatic.net. The views expressed in this material are solely those of the author and/or TheAutomatic.net and IBKR is not endorsing or recommending any investment or trading discussed in the material. This material is not and should not be construed as an offer to sell or the solicitation of an offer to buy any security. To the extent that this material discusses general market activity, industry or sector trends or other broad based economic or political conditions, it should not be construed as research or investment advice. To the extent that it includes references to specific securities, commodities, currencies, or other instruments, those references do not constitute a recommendation to buy, sell or hold such security. This material does not and is not intended to take into account the particular financial conditions, investment objectives or requirements of individual customers. Before acting on this material, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice.
In accordance with EU regulation: The statements in this document shall not be considered as an objective or independent explanation of the matters. Please note that this document (a) has not been prepared in accordance with legal requirements designed to promote the independence of investment research, and (b) is not subject to any prohibition on dealing ahead of the dissemination or publication of investment research.
Any trading symbols displayed are for illustrative purposes only and are not intended to portray recommendations.