Why use dict.keys?

Source: PEP 234, PEP 3106

Python 2's relatively useless dict.keys method exists for historical reasons. Originally, dicts weren't iterable. In fact, there was no such thing as an iterator; iterating over sequences worked by calling __getitem__, the element access method, with increasing integer indices until an IndexError was raised. To iterate over the keys of a dict, you had to call the keys method to get an explicit list of keys and iterate over that.

When iterators went in, dicts became iterable, because it was more convenient, faster, and all around better to say

for key in d:

than

for key in d.keys()

This had the side-effect of making d.keys() utterly superfluous; list(d) and iter(d) now did everything d.keys() did in a cleaner, more general way. They couldn't get rid of keys, though, since so much code already called it.

(At this time, dicts also got a __contains__ method, so you could say key in d instead of d.has_key(key). This was shorter and nicely symmetrical with for key in d; the symmetry is also why iterating over a dict gives the keys instead of (key, value) pairs.)

In Python 3, taking inspiration from the Java Collections Framework, the keys, values, and items methods of dicts were changed. Instead of returning lists, they would return views of the original dict. The key and item views would support set-like operations, and all views would be wrappers around the underlying dict, reflecting any changes to the dict. This made keys useful again.


On Python 3, use dct.keys() to get a dictionary view object, which lets you do set operations on just the keys:

>>> for sharedkey in dct1.keys() & dct2.keys():  # intersection of two dictionaries
...     print(dct1[sharedkey], dct2[sharedkey])

In Python 2.7, you'd use dct.viewkeys() for that.

In Python 2, dct.keys() returns a list, a copy of the keys in the dictionary. This can be passed around an a separate object that can be manipulated in its own right, including removing elements without affecting the dictionary itself; however, you can create the same list with list(dct), which works in both Python 2 and 3.

You indeed don't want any of these for iteration or membership testing; always use for key in dct and key in dct for those, respectively.