Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP


dictionary key completion #1533

gasagna opened this Issue · 8 comments

7 participants

Davide Lasagna Thomas Kluyver Anush S Thomas Hisch jnothman David P. Sanders Min RK
Davide Lasagna


Would it be possible to implement a feature like completion for dictionary keys? Something like:

mydict = {'amen':42, 'amenable':32, 0:'left'}
mydict[' (press tab)
'amen' 'amenable' 0

Thanks for the wonderful tool!


Thomas Kluyver

Yes-ish, although it might only be able to work in certain situations. Have a look at IPython.core.completer and IPython.core.completerlib.

Anush S

I am trying to add the above feature. I am confronted with a problem.

  • The logic conflicts with the tab-completion to populate lists and hence IPython.core.completer recognizes '[' as a delimiter.


  • Objects in the namespace -> xa, xb, xc, mydict = {'xy':10, 'xz':20}

    ---> xa xb xc

Any help/pointers to overcome this?


Thomas Hisch

Why don't you just look for the opening bracket followed by either ' or " in your completion logic. Thus, if you want to complete keys which are strings then you have to type mydict["TAB. This simplifies your completion logic because you don't have to ignore objects in your namespace.


I think this may only apply to literal string keys, in which case @thisch is right it should apply after [" or [' (assuming there's no intention to support .get(', etc.). It would be nice if this also supported the keys of numpy records!


I hacked up a solution to this (, which given a dict d = {'abcde': 1} would make suggestions like:

d[ -> 'abcde']
d[' -> abcde']
d["a -> abcde"]

It seems to work (add dict_key_completer to IPCompleter.matchers), but when completing a line with an odd number of quotes in it, readline adds a closing quote after tab completion, ignoring the fact that my completion already closes the quote (and the bracket). (rl.readline has an option called completion_suppress_quote, but readline doesn't, and I don't even know if pyreadline would add it.)

David P. Sanders

There is a nice solution that already exists which avoids the problem: use attribute access instead of key access.

from IPython.config import Config

d = {"H": 1, "He": 2}
d2 = Config(d)

Config takes the dictionary d and makes a new one d2 which has access via attributes: we can now do

print d.H
d.He = 3

And, magically, completion works!:


The only problem occurs when the keys themselves contain ..
This is the case, for example, in matplotlib.rcParams; see matplotlib/matplotlib#2249

Attribute access is (apart from this . problem) nicer, in my opinion, since it requires 5 fewer key strokes (counting Shift), and has less visual noise.


@dpsanders, apart from its inconvenience, your solution works only for keys that are valid identifiers. It also assumes that a copy of the dict is cheap, while for some Mapping objects, values may not be stored in memory.


I assume this can be closed.

Min RK minrk modified the milestone: 3.0, wishlist
Min RK minrk closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.