How to return dictionary keys as a list in Python 3.3
http://btmiller.com/2015/04/13/get-list-of-keys-from-dictionary-in-python-2-and-3.html
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:
$ python
>>> foo = { 'bar': "hello", 'baz': "world" }
>>> type(foo.keys())
<type 'list'>
>>> foo.keys()
['baz', 'bar']
>>> foo.keys()[0]
'baz'
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:
$ python3
>>> foo = { 'bar': "hello", 'baz': "world" }
>>> type(foo.keys())
<class 'dict_keys'>
>>> foo.keys()
dict_keys(['baz', 'bar'])
>>> foo.keys()[0]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'dict_keys' object does not support indexing
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:
$ python3
>>> foo = { 'bar': "hello", 'baz': "world" }
>>> type(list(foo.keys()))
<class 'list'>
>>> list(foo.keys())
['baz', 'bar']
>>> list(foo.keys())[0]
'baz'
And just for good measure, here it is in Python 2:
$ python
>>> foo = { 'bar': "hello", 'baz': "world" }
>>> type(list(foo.keys()))
<class 'list'>
>>> list(foo.keys())
['baz', 'bar']
>>> list(foo.keys())[0]
'baz'
http://stackoverflow.com/questions/16819222/how-to-return-dictionary-keys-as-a-list-in-python-3-3
I noticed something very weird - or let's say, something that is very different from Python 2.7 and older versions of Python 3 I believe. Previously, I could get dictionary keys, values, or items of a dictionary very easily as list:
Now, I get something like this in
I am wondering if there is a way to return a list as I showed it in the Python 2.7 example. Because now, I have to do something like
EDIT: Thanks, But there is another thing that bugs me now: I want to create a list of reversed dictionary keys and values to sort them by values. Like so (okay, this is a bad example, because the values are all 0 here)
However, in Python3 I get something like
Okay, sorry, I just figured out that you have to use a
This is really something one has to get used to |
||||
Try This wil convert the dict_keys object to a list. On the other hand, you should ask yourself whether or not it matters. The Pythonic way to code is to assume duck typing (if it looks like a duck and it quacks like a duck, it's a duck). the dict_keys object will act like a list for most purposes. For instance:
Obviously insertion operators may not work, but that doesn't make much sense for a list of dictionary keys anyway. |
|||||||||||||
|
sorted(newdict.items(),key=lambda x: x[1])
.newdict.items()
returns the key-value pairs as tuples (just like you're doing with the zip above).sorted
is the built-in sort function and it permits akey
parameter which should be a function that transforms each list element into the value which should be used to sort. – Chris May 29 '13 at 17:33