Skip to main content
added 32 characters in body; edited tags; edited title
Source Link
davidism
  • 125.7k
  • 30
  • 410
  • 346

What is the best way to get Get the first item from an iterable matchingthat matches a condition?

In Python, I would like to get the first item from a list matching a condition. It's important that the resulting method not process the entire list, which could be quite large. For example, the following function is adequate:

def first(the_iterable, condition = lambda x: True):
    for i in the_iterable:
        if condition(i):
            return i

This function could be used something like this:

>>> first(range(10))
0
>>> first(range(10), lambda i: i > 3)
4

However, I can't think of a good built-in / one-liner to let me do this (and. I don't particularly want to copy this function around if I don't have to). Any ideas?

(It's important that the resulting method not processIs there a built-in way to get the entire list, which could be quite large.)first item matching a condition?

What is the best way to get the first item from an iterable matching a condition?

In Python, I would like to get the first item from a list matching a condition. For example, the following function is adequate:

def first(the_iterable, condition = lambda x: True):
    for i in the_iterable:
        if condition(i):
            return i

This function could be used something like this:

>>> first(range(10))
0
>>> first(range(10), lambda i: i > 3)
4

However, I can't think of a good built-in / one-liner to let me do this (and I don't particularly want to copy this function around if I don't have to). Any ideas?

(It's important that the resulting method not process the entire list, which could be quite large.)

Get the first item from an iterable that matches a condition

I would like to get the first item from a list matching a condition. It's important that the resulting method not process the entire list, which could be quite large. For example, the following function is adequate:

def first(the_iterable, condition = lambda x: True):
    for i in the_iterable:
        if condition(i):
            return i

This function could be used something like this:

>>> first(range(10))
0
>>> first(range(10), lambda i: i > 3)
4

However, I can't think of a good built-in / one-liner to let me do this. I don't particularly want to copy this function around if I don't have to. Is there a built-in way to get the first item matching a condition?

Source Link
Chris Phillips
  • 12.2k
  • 3
  • 35
  • 45

What is the best way to get the first item from an iterable matching a condition?

In Python, I would like to get the first item from a list matching a condition. For example, the following function is adequate:

def first(the_iterable, condition = lambda x: True):
    for i in the_iterable:
        if condition(i):
            return i

This function could be used something like this:

>>> first(range(10))
0
>>> first(range(10), lambda i: i > 3)
4

However, I can't think of a good built-in / one-liner to let me do this (and I don't particularly want to copy this function around if I don't have to). Any ideas?

(It's important that the resulting method not process the entire list, which could be quite large.)