is there such a built-in funciton, similar to filter
cam.ac.uk at mh391.invalid
Sun Mar 5 11:57:54 CET 2006
> Beginner learning Python here. I know filter(lambda x: x > 3, [1, 2,
> 5, -3, 4, 8]) will give me a list [5, 4, 8]. What if I only need to
> find the first item in the list that returns Ture when applying the
> filter function. In this case, I only want to get the 5, and its index
> 2. Is there a built-in function, or function from a module for that?
> I think it is not hard to write a function for this. But knowing
> Python is "battery included", I thought I'd like to ask. Thanks for
> your help.
You can use a generator expression like this:
>>> items = [1, 2, 5, -3, 4, 8]
>>> ((index, item) for index, item in enumerate(items) if item > 3).next()
More information about the Python-list