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?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
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.
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:results in:
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.