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

  • Home
  • SEARCH
  • 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 298017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T06:41:35+00:00 2026-05-12T06:41:35+00:00

This question is in relation to another question asked here: Sorting 1M records I

  • 0

This question is in relation to another question asked here:
Sorting 1M records

I have since figured out the problem I was having with sorting. I was sorting items from a dictionary into a list every time I updated the data. I have since realized that a lot of the power of Python’s sort resides in the fact that it sorts data more quickly that is already partially sorted.

So, here is the question. Suppose I have the following as a sample set:

self.sorted_records = [(1, 1234567890), (20, 1245678903), 
                       (40, 1256789034), (70, 1278903456)]

t[1] of each tuple in the list is a unique id. Now I want to update this list with the follwoing:

updated_records = {1245678903:45, 1278903456:76}

What is the fastest way for me to do so ending up with

self.sorted_records = [(1, 1234567890), (45, 1245678903),
                       (40, 1256789034), (76, 1278903456)]

Currently I am doing something like this:

updated_keys = updated_records.keys()
for i, record in enumerate(self.sorted_data):
    if record[1] in updated_keys:
        updated_keys.remove(record[1])
        self.sorted_data[i] = (updated_records[record[1]], record[1])

But I am sure there is a faster, more elegant solution out there.

Any help?

* edit
It turns out I used bad exaples for the ids since they end up in sorted order when I do my update. I am actually interested in t[0] being in sorted order. After I do the update I was intending on resorting with the updated data, but it looks like bisect might be the ticket to insert in sorted order.
end edit *

  • 1 1 Answer
  • 1 View
  • 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-12T06:41:35+00:00Added an answer on May 12, 2026 at 6:41 am

    Since apparently you don’t care about the ending value of self.sorted_records actually being sorted (you have values in order 1, 45, 20, 76 — that’s NOT sorted!-), AND you only appear to care about IDs in updated_records that are also in self.sorted_data, a listcomp (with side effects if you want to change the updated_record on the fly) would serve you well, i.e.:

    self.sorted_data = [(updated_records.pop(recid, value), recid) 
                        for (value, recid) in self.sorted_data]
    

    the .pop call removes from updated_records the keys (and corresponding values) that are ending up in the new self.sorted_data (and the “previous value for that recid“, value, is supplied as the 2nd argument to pop to ensure no change where a recid is NOT in updated_record); this leaves in updated_record the “new” stuff so you can e.g append it to self.sorted_data before re-sorting, i.e I suspect you want to continue with something like

    self.sorted_data.extend(value, recid 
                            for recid, value in updated_records.iteritems())
    self.sorted_data.sort()
    

    though this part DOES go beyond the question you’re actually asking (and I’m giving it only because I’ve seen your previous questions;-).

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

Sidebar

Related Questions

This question comes on the heels of the question asked here . The email
This is in relation to this question I am hosting this WCF service in
In relation to this question on Using OpenGL extensions , what's the purpose of
This question is kind of an add-on to this question In C#, a switch
This question and answer shows how to send a file as a byte array
This question would probably apply equally as well to other languages with C-like multi-line
This question is a follow up to my previous question about getting the HTML
(This question is over 6 years old and probably no longer has any relevance.)
This question is about removing sequences from an array, not duplicates in the strict
This question is the other side of the question asking, How do I calculate

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.