I had similar problem when my list was very long. Comprehension or filter-based solutions would go through the whole list. Instead itertools.takewhile will break the loop once the condition is false for the first time:
from itertools import takewhile
def f(l, b): return len([x for x in takewhile(lambda x: x[1] <= b, enumerate(l))])
l = [0.5, 0.3, 0.9, 0.8]
f(l, 0.7)
I know there are already plenty of answers, but I sometimes I feel that the word pythonic is translated into 'one-liner'.
When I think a better definition is closer to this answer:
"Exploiting the features of the Python language to produce code that is clear, concise and maintainable."
While some of the above answers are concise, I do not find them to be clear and it would take a newbie programmer a while to understand, therefore not making them extremely maintainable for a team built of many skill levels.
l = [0.5, 0.3, 0.9, 0.8]
def f(l, x):
for i in l:
if i >x: break
return l.index(i)
f(l,.7)
or
l = [0.5, 0.3, 0.9, 0.8]
def f(l, x):
for i in l:
if i >x: return l.index(i)
f(l,.7)
I think the above is easily understood by a newbie and is still concise enough to be accepted by any veteran python programmer.
If you are happy to use numpy (imported as np here), then the following will work on general lists (sorted or unsorted):
np.argwhere(np.array(searchlist)>x)[0]
or if you need the answer as a integer index:
np.argwhere(np.array(searchlist)>x)[0][0]
2) NUMPY SEARCHSORTED, sorted lists (very efficient for searching lists within a list)
However, if your search list [l1,l2,...] is sorted, it is much cleaner and nicer to use the function np.searchsorted:
np.searchsorted(searchlist,x)
The nice thing about using this function is that as well as searching for a single value x within the search list [l1,l2,...], you can also search for a list of values [x1,x2,x3...xn] within your search list (i.e. x can be a list too, and it is extremely efficient relative to a list comprehension in this case).