The Python dictionary keys() method returns an object which contains the keys in a dictionary.
Dictionary. Dictionaries are used to store data values in key:value pairs. A dictionary is a collection which is ordered*, changeable and do not allow duplicates. As of Python version 3.7, dictionaries are ordered.
With CPython 2.7, using dict() to create dictionaries takes up to 6 times longer and involves more memory allocation operations than the literal syntax. Use {} to create dictionaries, especially if you are pre-populating them, unless the literal syntax does not work for your case.
The reason is because a dictionary is a lookup, while a list is an iteration. Dictionary uses a hash lookup, while your list requires walking through the list until it finds the result from beginning to the result each time.
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.
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.
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()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With