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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T17:22:34+00:00 2026-06-16T17:22:34+00:00

I have two lists both containing numbers, and I want to check if the

  • 0

I have two lists both containing numbers, and I want to check if the numbers from one list are numbers between every two numbers from the other list? The “other” list has ordered numbers so that every two numbers is an interval like 234, 238, 567, 569, 2134, 2156.

ListA = [200, 765, 924, 456, 231]

ListB = [213, 220, 345, 789, 12, 45]

I want to check if ListA[0:] is a number between either ListB[0:1] or [2:3] etc.

I have tried this:

for i in range(0,len(ListB), 2):
    for x in ListA:
        if i < x < (i:i+2):
            print 'within range'

But I get a syntax error for the “i:i+2” 🙁

  • 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-16T17:22:35+00:00Added an answer on June 16, 2026 at 5:22 pm

    I think this is what you want, although it’s not that clear:

    for i in range(0, len(ListB), 2):
        for x in ListA:
            if ListB[i] < x < ListB[i+1]:
                print 'within range'
    

    You’re right in your intuition that you can i:i+2 sort of represents a “range”, but not quite like that. That syntax can only be used when slicing a list: ListB[i:i+2] returns a smaller list with just 2 elements, [ListB[i], ListB[i+1]], just as you expect, but i:i+2 on its own is illegal. (If you really want to, you can write slice(i, i+2) to mean what you’re aiming at, but it’s not all that useful here.)

    Also, from your description, you want to compare x to the values ListB[i] and ListB[i+1], not just i and i+1 as in your code.

    If you wanted to use the slice to generate a range, you could do so, but only indirectly, and it would actually be clumsier and worse in most ways:

    for i in range(0, len(ListB), 2):
        brange = range(*ListB[i:i+2])
        for x in ListA:
            if x in brange:
                print 'within range'
    

    But this is checking for a half-open range rather than an open range, and it requires creating the list representing the range, and walking over the whole thing, and it will only work for integers. So, it’s probably not what you want. Better to just explicitly use ListB[i] < x < ListB[i+1].

    You might want to consider whether there’s a more readable way to build a list of pairs of elements from ListB. Without yet knowing about itertools and list comprehensions or generator expressions (I assume), you’d probably have to write something pretty verbose and explicit, but it might be useful practice anyway.

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

Sidebar

Related Questions

I have two dropdown lists, one containing a list of countries and one for
I have two lists containing unicode strings. I'm trying to remove every element from
I have two lists. They are sortable but not connected (elements of list one
I have two lists of files, one from a local drive and one from
I have two list containing List<MyObj>. and MyObj has a String ID member. I
I have two lists. The types of both lists inherit from the same base
I have two lists. Both containing the same values: QStringList filePaths; filePaths.append(C:/backup); filePaths.append(C:/download/file1); filePaths.append(D:);
I Have two Lists each containing rows from sql query containing 2 elements(columns) operator
I have two Lists of dictionaries. Both are not not null. How to merge
I have two files which both contain a list of words. Is there an

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.