Why use dict.keys?

Question:

I recently wrote some code that looked something like this:

# dct is a dictionary
if "key" in dct.keys():

However, I later found that I could achieve the same results with:

if "key" in dct:

This discovery got me thinking and I began to run some tests to see if there could be a scenario when I must use the keys method of a dictionary. My conclusion however is no, there is not.

If I want the keys in a list, I can do:

keys_list = list(dct)

If I want to iterate over the keys, I can do:

for key in dct:
    ...

Lastly, if I want to test if a key is in dct, I can use in as I did above.

Summed up, my question is: am I missing something? Could there ever be a scenario where I must use the keys method?…or is it simply a leftover method from an earlier installation of Python that should be ignored?

Asked By: user2555451

||

Answers:

Assuming you’re not using Python 3, list(dct) is equivalent to dct.keys(). Which one you use is a matter of personal preference. I personally think dct.keys() is slightly clearer, but to each their own.

In any case, there isn’t a scenario where you “need” to use dct.keys() per se.

In Python 3, dct.keys() returns a “dictionary view object”, so if you need to get a hold of an unmaterialized view to the keys (which could be useful for huge dictionaries) outside of a for loop context, you’d need to use dct.keys().

Answered By: tom

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.

Answered By: Martijn Pieters

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.

Answered By: user2357112

key in dict
is much faster than checking
key in dict.keys()

Answered By: Acton
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.