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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:56:10+00:00 2026-05-27T07:56:10+00:00

I have a list of many elements. I care about two of its elements,

  • 0

I have a list of many elements.

I care about two of its elements, a and b.

I don’t know the order of the list, nor do I want to sort it.

Is there a nice one-liner that will return True if a occurs before b and false otherwise?

  • 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-27T07:56:11+00:00Added an answer on May 27, 2026 at 7:56 am

    Edit: Rewritten to check more cases

    OK, so this problem needs a bit more work. Mark Byers is completely right in that my first test only covered cases where the result would be True. This is especially relevant because we need exception handlers for the other solutions. So I’ve gone into a bit more detail:

    stmts = {
    "Mark Byers: ": "x = l.index(a) < l.index(b)",
    "jcollado: ": """try:
        x = bool(l.index(b, l.index(a)))
    except ValueError:
        x = False""",
    "Greg Hewgill: ": """try:
       x = b in l[l.index(a):]
    except ValueError:
       x = False"""
    }
    
    setups = ["a = 80; b = 90; l = list(range(100))", 
              "a = 5; b = 10; l = list(range(100))", 
              "a = 90; b = 80; l = list(range(100))",
              "a = 10; b = 5; l = list(range(100))"]
    
    import timeit
    for se in setups:
        print(se)
        for st in stmts:
            print(st, timeit.timeit(stmt=stmts[st], setup=se))
        print()
    

    results in:

    a = 80; b = 90; l = list(range(100))
    Mark Byers:  5.760545506106019
    Greg Hewgill:  3.454101240451526     # Tie!
    jcollado:  3.4574156981854536        # Tie!
    
    
    a = 5; b = 10; l = list(range(100))
    Mark Byers:  1.0853995762934794      # Close runner-up!
    Greg Hewgill:  1.7265326426395209
    jcollado:  1.0528704983320782        # Winner!
    
    a = 90; b = 80; l = list(range(100))
    Mark Byers:  5.741535600372806
    Greg Hewgill:  3.623253643486848     # Winner!
    jcollado:  4.567104188774817
    
    a = 10; b = 5; l = list(range(100))
    Mark Byers:  1.0592141197866987      # Winner!
    Greg Hewgill:  4.73399648151641
    jcollado:  4.77415749512712
    

    So the efficiency gain from jcollado’s method is mostly eaten up by the cost of the exception handler (expecially if it triggers). All three solutions win (or tie with the winner) half of the time, so which method works best on your actual data is hard to say. Perhaps you might want to go with the one that’s easiest to read.

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

Sidebar

Related Questions

I have an array list called str, i want to see how many elements
I have a List<int> and need to count how many elements with (value <
I have a list that contains many sub-lists of 3 elements each, like: my_list
I don't know if the title express what I want. I have a ListBox
i have a double linked list with many list elements. so i can use
I have many li elements in an ul list like below: <li id=filesOrderArray_dynamic_number>> <a
I have a list of (many) employees in Excel/csv who take sick days, listed
Background I have a list . This list has many objects. Each object has
I have a list view with many rows and have it set so that
I have a list and a column; the column might have many values, and

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.