[Python] Ordered data structure with O(1) lookup and allowed duplicates in python

Discussão em 'Python' iniciado por Stack, Março 1, 2025.

  1. Stack

    Stack Membro Participativo

    I'm looking for a data structure that will allow me to test if an element exists within it in O(1) time as such:

    if value in my_data_structure:
    pass


    However, I need it to be able to keep its order of inserted elements, so I can access them via index or any other way.

    The closest I found from a similar question was a multiset, to which in Python a Counter is the closest, but those don't keep any semblance of order and the duplicates are 'aggregated'.

    Does anything like this exist at all, even outside of Python, so I could program it myself?

    Continue reading...

Compartilhe esta Página