grab dict keys/values without iterating ?!

Ian Kelly ian.g.kelly at gmail.com
Wed Dec 11 20:05:29 EST 2013


On Wed, Dec 11, 2013 at 6:02 PM, Ian Kelly <ian.g.kelly at gmail.com> wrote:
> This is what I did not so long ago when writing a utility for
> typeahead lookup, except that to save some space and time I only
> nested the dicts as deeply as there were still multiple entries.  As
> an example of what the data structure looked like:
>
> lookups = {
>     'a': {
>         'l': {
>             'g': 'algebra',
>             'p': 'alphanumeric',
>         },
>         's': 'asterisk',
>     },
>     'b': 'bobcat',
>     ...
> }
>
> It does make the update process more complicated though, as adding new
> words potentially requires existing words to be nested deeper than
> they are currently.

And I'm simplifying that a bit, because I also included at each node
the preferred (in my case, the first alphabetically) completion for
that prefix, to avoid the need to iterate over the subtree.



More information about the Python-list mailing list