Inverted OrderedDict TypeError

I have an ordered dictionary, and I'm trying to go through it in reverse order, mainly starting from the last (key, value).

dicts = OrderedDict()
...
for key, value in reversed(dicts.items()):

The above code works fine when I use PyCharm with Python 3.5.1, but when I put it in code games using their Python 3 interpreter (not sure which version), I get the following error to implement the for loop:

TypeError: argument to reversed() must be a sequence

Why is this error happening and how can I fix it?

+4
source share
1 answer

According to the docs , it OrderedDict.items()is only supported reversedfrom 3.5, so I expect this to be an older version.

, , - :

for key, value in reversed(list(dicts.items())):
     ...

NB: , dicts.items() , list(dicts.items()) dicts. , dicts O (N), .

+9

Source: https://habr.com/ru/post/1672382/


All Articles