You could put all the prefixes of the inserted keys to the dict, so for key foo
you would insert f
, fo
and foo
. You would have O(1) lookup, but you would spend time on preprocessing (O(k), where k is a key length), and waste lots of memory:
def insert_with_prefixes(key, value, dict_):
prefixes = (key[:i+1] for i in xrange(len(key)))
dict_.update((prefix, value) for prefix in prefixes)
For everyday use I would go (and I go) with the method in arshajii's answer. And of course, have in mind possible many collisions for short prefixes (here: "h"
):
>>> a = {}
>>> insert_with_prefixes('hello', 'world', a)
>>> insert_with_prefixes('homo', 'sapiens', a)
>>> a
{'h': 'sapiens', 'hom': 'sapiens', 'homo': 'sapiens', 'ho': 'sapiens',
'hel': 'world', 'hell': 'world', 'hello': 'world', 'he': 'world'}