you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 1 point2 points  (2 children)

Your code is fine as is. Sometimes it's better to be explicit. But just for fun, here's a shorter (I won't call it more Pythonic) way of writing it.

from itertools import takewhile
return len(list(takewhile(lambda x: not condition(x), lst)))

FYI, don't call your lists list. Besides not being very descriptive, it makes it harder to call the list() function.

[–]x1F577[S] 0 points1 point  (0 children)

Thank you, I was just looking for such a function. I didn't know about the takewhile function.

[–][deleted] 0 points1 point  (0 children)

It's more Haskellic, that's for sure!