What would be the most Pythonic way to find the first index in a list that is greater than x?
For example, with
list = [0.5, 0.3, 0.9, 0.8]
filter(lambda x: x>.7, seq)[0]
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.
I think writing dumb code is a positive.
I had similar problem when my list was very long. comprehension or filter -based solutions would go thru whole list. itertools.takewhile will break the loop once condition become false 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)
next(x[0] for x in enumerate(L) if x[1] > 0.7)
>>> f=lambda seq, m: [ii for ii in xrange(0, len(seq)) if seq[ii] > m][0]
>>> f([.5, .3, .9, .8], 0.7)
2
if list is sorted then bisect.bisect_left(alist, value)
is faster for a large list than next(i for i, x in enumerate(alist) if x >= value)
.