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 7175359
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:16:29+00:00 2026-05-28T16:16:29+00:00

I am interested in a dict implementation for Python that provides an iterating interface

  • 0

I am interested in a dict implementation for Python that provides an iterating interface to sorted values. I.e., a dict with a “sortedvalues()” function.

Naively one can do sorted(dict.values()) but that’s not what I want. Every time items are inserted or deleted, one has to run a full sorting which isn’t efficient.

Note that I am not asking about key-sorted dict either (for that question, there are excellent answers in Key-ordered dict in Python and Python 2.6 TreeMap/SortedDictionary?).

  • 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-05-28T16:16:30+00:00Added an answer on May 28, 2026 at 4:16 pm

    The problem is that you need to sort or hash it by keys to get reasonable insert and lookup performance. A naive way of implementing it would be a value-sorted tree structure of entries, and a dict to lookup the tree position for a key. You need to get deep into updating the tree though, as this lookup dictionary needs to be kept correct. Essentially, as you would do for an updatable heap.

    I figure there are too many options to make a resonable standard library option out of such a structure, while it is too rarely needed.

    Update: a trick that might work for you is to use a dual structure:

    1. a regular dict storing the key-value pairs as usual

    2. any kind of sorted list, for example using bisect

    Then you have to implement the common operations on both: a new value is inserted into both structures. The tricky part are the update and delete operations. You use the first structure to look up the old value, delete the old value from the second structure, then (when updating) reinsert as before.

    If you need to know the keys too, store (value, key) pairs in your b list.

    Update 2: Try this class:

    import bisect
    class dictvs(dict):
        def __init__(self):
            self._list = []
    
        def __setitem__(self, key, value):
            old = self.get(key)
            if old is None:
                bisect.insort(self._list, value)
                dict.__setitem__(self, key, value)
            else:
                oldpos = bisect.bisect_left(self._list, old)
                newpos = bisect.bisect_left(self._list, value)
                if newpos > oldpos:
                    newpos -= 1
                    for i in xrange(oldpos, newpos):
                        self._list[i] = self._list[i + 1]
                else:
                    for i in xrange(oldpos, newpos, -1):
                        self._list[i] = self._list[i - 1]
                self._list[newpos] = value
                dict.__setitem__(self, key, value)
    
        def __delitem__(self, key):
            old = self.get(key)
            if old is not None:
                oldpos = bisect.bisect(self._list, old)
                del self._list[oldpos]
            dict.__delitem__(self, key)
    
        def values(self):
            return list(self._list)
    

    It’s not a complete dict yet I guess. I havn’t tested deletions, and just a tiny update set. You should make a larger unit test for it, and compare the return of values() with that of sorted(dict.values(instance)) there. This is just to show how to update the sorted list with bisect

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does native built-in python dict guarantee that the keys() and values() lists are ordered
I've got a dict that has a whole bunch of entries. I'm only interested
im interested i just finished writing a program and one of the functions that
Im interested in a way one service activate users. The scenario is that I
im interested how many api calls per second or per minute i can do
When writing unit tests, there are cases where one can create an Assert for
I want to write a function that reads a file and counts the number
I have a Python datetime timestamp and a large dict (index) where keys are
Interested in people's opinion. You have an application server running 3/4 services that do
Interested in using libxml2dom in a Python script of mine to (obviously) parse some

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.