Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syntax sugar for querying a Python list which element occurs first

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?

like image 487
2rs2ts Avatar asked Dec 07 '11 08:12

2rs2ts


1 Answers

In the interests of diversity, you could also:

b in l[l.index(a):]

This will be True if a == b. If you know that a != b,

b in l[l.index(a) + 1:]
like image 68
Greg Hewgill Avatar answered Sep 28 '22 01:09

Greg Hewgill