dict_key and dict_value to list performances

70 views Asked by At

With python 2.x we can do this:

>>> d = {0:'foo', 1:'bar'}
>>> d.keys()
[0, 1]
>>> d.keys()[0]
0
>>> d.values()
['foo', 'bar']
>>> d.values()[0]
'foo'

With python 3.x, .keys() return dict_key and .values() return dict_value. I guess these view objects are most performant than direct list render in python 2.x (like generator ?).

But, to access dict key/dict value by index we have to use list():

>>> d = {0:'foo', 1:'bar'}
>>> d.keys()
dict_keys([0, 1])
>>> d.values()
dict_values(['foo', 'bar'])
>>> list(d.values())[0]
'foo'
>>> list(d.keys())[0]
0

Is there a way to access them by index (0, 1, [...] 999] without make a complete list() of keys/values to access one index ? (I work with very big dictionnary)

This question is about performance. Not about how to do it. Duplicate flag is not pertinant.

0

There are 0 answers