Search⌘ K
AI Features

Slowing Down dict Lookups

Explore how Python's dictionary lookup functions work, particularly the difference between specialized string-key lookups and generic lookups. Understand the causes of slowed dictionary access when non-string keys are introduced, and learn how this behavior impacts your code's performance.

We'll cover the following...

Let’s show you how to slow down dict lookup at will.

Python 3.5
some_dict = {str(i): 1 for i in range(1000000)}
another_dict = {str(i): 1 for i in range(1000000)}
>>> timeit.timeit("some_dict['5']", number=1000, globals=globals())
6.216699989636254e-05
>>> some_dict[1] = 1
>>> timeit.timeit("some_dict['5']", number=1000, globals=globals())
9.339500002170098e-05
>>> timeit.timeit("another_dict['5']", number=1000, globals=globals())
6.14590001077886e-05
>>> another_dict[1]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 1
>>> timeit.timeit("another_dict['5']", number=1000, globals=globals())
8.72460000209685e-05

Try it out in the terminal below:

Terminal 1
Terminal
Loading...

Why are the same lookups becoming slower?

Explanation

  • CPython has a generic dictionary lookup function that handles all types of
...