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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T05:54:31+00:00 2026-06-11T05:54:31+00:00

I having a list of tuple as describes below (This tuple is sorted in

  • 0

I having a list of tuple as describes below (This tuple is sorted in decreasing order of the second value):

from string import ascii_letters
myTup = zip (ascii_letters, range(10)[::-1])
threshold = 5.5

>>> myTup
[('a', 9), ('b', 8), ('c', 7), ('d', 6), ('e', 5), ('f', 4), ('g', 3), ('h', 2), \
('i', 1), ('j', 0)]

Given a threshold, what is the best possible way to discard all tuples having the second value less than this threshold.

I am having more than 5 million tuples and thus don’t want to perform comparison tuple by tuple basis and consequently delete or add to another list of tuples.

  • 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-11T05:54:33+00:00Added an answer on June 11, 2026 at 5:54 am

    Since the tuples are sorted, you can simply search for the first tuple with a value lower than the threshold, and then delete the remaining values using slice notation:

    index = next(i for i, (t1, t2) in enumerate(myTup) if t2 < threshold)
    del myTup[index:]
    

    As Vaughn Cato points out, a binary search would speed things up even more. bisect.bisect would be useful, except that it won’t work with your current data structure unless you create a separate key sequence, as documented here. But that violates your prohibition on creating new lists.

    Still, you could use the source code as the basis for your own binary search. Or, you could change your data structure:

    >>> myTup
    [(0, 'a'), (1, 'b'), (2, 'c'), (3, 'd'), (4, 'e'), (5, 'f'), 
     (6, 'g'), (7, 'h'), (8, 'i'), (9, 'j')]
    >>> index = bisect.bisect(myTup, (threshold, None))
    >>> del myTup[:index]
    >>> myTup
    [(6, 'g'), (7, 'h'), (8, 'i'), (9, 'j')]
    

    The disadvantage here is that the deletion may occur in linear time, since Python will have to shift the entire block of memory back… unless Python is smart about deleting slices that start from 0. (Anyone know?)

    Finally, if you’re really willing to change your data structure, you could do this:

    [(-9, 'a'), (-8, 'b'), (-7, 'c'), (-6, 'd'), (-5, 'e'), (-4, 'f'), 
     (-3, 'g'), (-2, 'h'), (-1, 'i'), (0, 'j')]
    >>> index = bisect.bisect(myTup, (-threshold, None))
    >>> del myTup[index:]
    >>> myTup
    [(-9, 'a'), (-8, 'b'), (-7, 'c'), (-6, 'd')]
    

    (Note that Python 3 will complain about the None comparison, so you could use something like (-threshold, chr(0)) instead.)

    My suspicion is that the linear time search I suggested at the beginning is acceptable in most circumstances.

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

Sidebar

Related Questions

I am actually having a list of text boxes. I am using below code
I have some troubles with a method having a typed List parameter, inherited from
I use LINQ on a Dictionary<string, IList<ID>> like this: var searchCategories = new List
Having some trouble with this code. Trying to return a tuple of tuples (coordinates)
I have a list of N positive numbers sorted in ascending order, L[0] to
I have list of 10 elements having a tuple of 2 elements I want
I am having list of char array by that I want to store all
Having a list of data object and something visual to represent each, where would
Im having a list of student names in a table , in the same
Consider having a list - Ferrari, Mclaren, Red Bull on A2, A5, A8 cells,

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.