Get a List of Keys From a Dictionary in Both Python 2 and Python 3
It was mentioned in an earlier post that there is a difference in how the keys()
operation behaves between Python 2 and Python 3. If you’re adapting your Python 2 code to Python 3 (which you should), it will throw a TypeError
when you try to operate on keys()
like a list. So, if you depend on getting a list returned from keys()
, here’s how to make it work for both Python 2 and Python 3.
In Python 2, simply calling keys()
on a dictionary object will return what you expect:
That’s great, however, in Python 3, keys()
no longer returns a list, but a view object:
The objects returned by
dict.keys()
,dict.values()
anddict.items()
are view objects. They provide a dynamic view on the dictionary’s entries, which means that when the dictionary changes, the view reflects these changes.
The dict_keys
object is an iterator and looks a lot more like a set
than a list
. So using the same call in Python 3 would produce this result:
The TypeError
can be avoided and compatibility can be maintained by simply converting the dict_keys
object into a list which can then be indexed as normal in both Python 2 and Python 3:
And just for good measure, here it is in Python 2:
comments powered by Disqus