Asset Classes

Free investment financial education

More Campus Resources

Useful Tools and Information

Language

Multilingual content from IBKR

# Why Defining Constants Is Important – a Python Example

###### Posted May 24, 2023 at 11:36 am
TheAutomatic.net

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.

### Naive approach

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.

#### Join The Conversation

If you have a general question, it may already be covered in our FAQs. If you have an account-specific question or concern, please reach out to Client Services.

###### Disclosure: Interactive Brokers

Information posted on IBKR Campus that is provided by third-parties does NOT constitute a recommendation 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 its permission. The views expressed in this material are solely those of the author and/or TheAutomatic.net and Interactive Brokers 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 buy or sell any security. It should not be construed as research or investment advice or a recommendation to buy, sell or hold any security or commodity. 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.

This website uses cookies to collect usage information in order to offer a better browsing experience. By browsing this site or by clicking on the "ACCEPT COOKIES" button you accept our Cookie Policy.