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

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy