Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7847563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T17:56:28+00:00 2026-06-02T17:56:28+00:00

I have a dictionary ‘vcomments’ where the keys are non-sequential integers. When looping through

  • 0

I have a dictionary ‘vcomments’ where the keys are non-sequential integers. When looping through the keys I need to do so in sorted, or reverse-sorted, order. Currently I use

for key_pt in sorted(self.view.vcomments.iterkeys()):

but I also need to find those keys (or the next key) that are beyond, or before, a certain number:

    if direction == 'down':
        sorted_pts = (key_pt for key_pt in sorted(self.view.vcomments.iterkeys()) if key_pt > curr_pt)
    else:
        sorted_pts = (key_pt for key_pt in reversed(sorted(self.view.vcomments.iterkeys())) if key_pt < curr_pt)
    try:
        next_pt = sorted_pts.next()
    except StopIteration:
  1. Is it possible for me to create an iterator class (using the iterator protocol) that will store the dictionary and enable me to loop through them in either forward, or reverse, order? I’m assuming/guessing that I might need to first assign an attribute-value that will indicate whether the next loop should be forward/reverse.

  2. Can I include a generator-function (nested) within my iterator class that will enable me to retrieve the next key; that is, beyond or before a supplied integer-number?

  3. Similarly, will there be a way for me to supply begin-and-end points and retrieve all keys that fall between these values (in sorted order)?

I apologise for asking three (although related) questions – an answer to the first would give me a start. And I’m not rude enough to expect a full solution, just an indication whether these are feasible targets for me.

Added: and I would still need to be able to retrieve a single, specific dictionary-item by its key.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T17:56:30+00:00Added an answer on June 2, 2026 at 5:56 pm

    I think the best data structure for your needs here is a skip list. I’ve never implemented one — always wanted to — but it looks to me like this has all of the things you need.

    1. A skip list stores its items in sorted order. Making the base list a doubly linked list will allow forward and reverse iteration in O(n).

    2. A skip list allows O(log n) insertions, modifications, deletions, and searches. That’s not quite so fast as a dictionary, but it seems to me that if you need the items stored in sorted order, a dictionary is going to give you trouble — even an OrderedDict, unless you are very rarely adding keys.

    3. With some modifications described in the wikipedia article above, even indexed access can be implemented in O(log n).

    There’s one implementation in Python here — there are probably others.

    However, some of your comments suggest that you may be content with simply iterating over a sorted copy of your dictionary, and you’re just trying to clean up the above code. So here is one way to go about it. This is pretty naive, but it’s a starting point. This assumes you’re totally fine with O(n) search times and O(n log n) iteration times, which are both suboptimal…

    >>> class SortIterDict(dict):
    ...     def __iter__(self):
    ...         return iter(sorted(super(SortIterDict, self).__iter__()))
    ...     def __reversed__(self):
    ...         return reversed(tuple(iter(self)))
    ...     def get_next(self, n):
    ...         return next((x for x in iter(self) if x > n), None)
    ...     def get_prev(self, n):
    ...         return next((x for x in reversed(self) if x < n), None)
    ... 
    >>> d = SortIterDict({'d':6, 'a':5, 'c':2})
    >>> list(d)
    ['a', 'c', 'd']
    >>> list(reversed(d))
    ['d', 'c', 'a']
    >>> d.get_next('b')
    'c'
    >>> d.get_prev('b')
    'a'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a dictionary (keys are integers, values are float). I would now ask
I have a dictionary. I need to create buttons with keys name, and clicked
I have a dictionary where keys are strings, and values are integers. stats =
I have a dictionary which is sorted in descending order.Each string (key) is a
I have a dictionary dict1['a'] = [ [1,2], [3,4] ] and need to generate
I want to have Dictionary that would be 'Observable' in order to throw events
Assume we have dictionary that translates strings into numbers. How to reverse it into
The problem: I have Dictionary<String, String> that I need an alias to, but I
I have a dictionary of objects that need to be cleaned up before they
I have a dictionary of 200,000 items (the keys are strings and the values

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.