OrderedDict in python 3 - how to get the keys in order?

Viewed 6660

In python 2 when using an OrderedDict I was able to get the keys in the order they were inserted by simply using the keys method that returned a list. In python 3 however:

rows = OrderedDict()
rows[0]=[1,2,3]
rows[1]=[1,2,3]
image = [rows[k] for k in rows.keys()[:2]]

I get:

Traceback (most recent call last):
  File "<input>", line 1, in <module>
TypeError: 'odict_keys' object is not subscriptable

I certainly can do list(rows)[:2] as advised for instance here - but is this guaranteed to get me the keys in order ? Is this the one right way ?

UPDATE: the python 2 code would be better as:

image = [v for v in rows.values()[:2]]

which of course still blows with the same error on python 3

1 Answers
Related