Why defining constants is important – a Python example

python key parameter


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).

python dictionary items

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)

python list comprehension max value in dict

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)

python constant increase efficiency

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.

That’s it for this post! Please check out my other Python posts by clicking here.