Skip to content

reversed() does not work as expected for ucollections.OrderedDict #5371

@nevercast

Description

@nevercast

Since I expected OrderedDict to be a doubly linked list, and I needed the last element I wanted to use reversed to grab the last element in the OrderedDict, it didn't work as expected so I wrote some test code.

from collections import OrderedDict
from random import randrange

source = list(set(randrange(2000) for _ in range(2000)))
od = OrderedDict([(k, 1) for k in source])

print(list(od) == source) # True 

# CPython: True
# MicroPython: KeyError or False
last_key = next(reversed(od))
print(last_key == source[-1]) 


# CPython: True
# MicroPython: KeyError
print(list(reversed(source)) == list(reversed(od))) # True

Sure enough, it does not work expected. Producing either KeyErrors or just bad values.

Metadata

Metadata

Assignees

No one assigned

    Labels

    py-coreRelates to py/ directory in source

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions