Skip to content

Instantly share code, notes, and snippets.

@Tadaboody
Created April 21, 2019 11:01
Show Gist options
  • Save Tadaboody/09ec7cad81751e5425484f9530773c09 to your computer and use it in GitHub Desktop.
Save Tadaboody/09ec7cad81751e5425484f9530773c09 to your computer and use it in GitHub Desktop.
Returns the element that satisfies the predicate, if exists
from typing import TypeVar, Sequence, Optional, Callable
T = TypeVar("T")
def find_in_sequence(
sequence: Sequence[T], predicate: Callable[[T], bool]
) -> Optional[T]:
"""Returns the element that satisfies the predicate, if exists"""
return next((element for element in sequence if predicate(element)), None)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment