CPython’s dict
implementation is in fact optimized for string key lookups. There are two different functions, lookdict
and lookdict_string
(lookdict_unicode
in Python 3), which can be used to perform lookups. Python will use the string-optimized version until a search for non-string data, after which the more general function is used. You can look at the actual implementation by downloading CPython’s source and reading through dictobject.c
.
As a result of this optimization, lookups are faster when a dict
has all string keys.