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

The Archive Base Latest Questions

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

I have a sorted list like this s = [1 , 4 ,6 ,

  • 0

I have a sorted list like this

s = [1 , 4 ,6 , 9  ,10 ]

I want to know if either a number is present in the list or if it is present in between two numbers. If it is present in between two numbers, I want to print them out.

Right now my code looks like this

for x in s:
   if b == x: \\ b is the number
       print b
   elif b > x and b < s[s.index(x) + 1] and s.index(x) < len(s):
       print b , s[s.index(x) + 1]

Is there a better way to do so?

  • 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-11T13:09:32+00:00Added an answer on June 11, 2026 at 1:09 pm

    bisect module does exactly that:

    s = [1 , 4 ,6 , 9  ,10 ]
    import bisect
    
    x = 5
    n = bisect.bisect_left(s, x)
    if s[n:n+1] == [x]:
        print x, 'is in the list'
    else:
        print x, 'comes between', s[n-1:n], 'and', s[n:n+1]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list that is sorted and I want to know how many
I have sorted list of datetime.time and want to create intervals like. If I
I have sorted list of strings that I move between php and java. to
I have a list of links and want to know the joined path/cycle. My
I have got a std::list< std::pair<std::string,double> > , which I know is sorted according
I have a list like this: [(ip1, video1, 12345.00000),(ip1, video1, 12346.12362),(ip1, video1, 12347.12684),(ip1, video2,12367.12567),(ip2,
I have a list of dicts, something like this: test_data = [ { 'offset':0,
I have a list of tuples that look something like this: (Person 1,10) (Person
I have a list of tuples that looks like this: [(334.99972431901307, 0.0), (335.00088248902574, 0.0),
I have a list of tuples, like this: [(time1, hashusi, servername, uri, referrer, useragent),

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.