use Python to quickly generate autocomplete suggestions

algorithm, autocomplete, python

Solution

I guess the fastest and most space-efficient data structure for this kind of problems is to use a prefix tree. After you have parsed your collection of words into the tree, the lookup time should be pretty fast. There even seems to be a python implementation out there.

Problem

I have a set `all_words` of about 6.5 million words. How can I use Python to quickly generate a list of words that begin with a given string? Obviously, I can do something like ``` def completions(word_start): ell = len(word_start) return [w for w in all_words if w[: ell] == word_start] ``` This works but it takes on the order of a second. What is a faster way to generate the full list?

Original source