Skip to content

Instantly share code, notes, and snippets.

@kmicinski
Created September 15, 2022 19:23
Show Gist options
  • Save kmicinski/6228f74231ff2701e85ce1e664c2094b to your computer and use it in GitHub Desktop.
Save kmicinski/6228f74231ff2701e85ce1e664c2094b to your computer and use it in GitHub Desktop.
def take_first_n(lst, f, n):
ret_lst = []
for element in lst:
if (n == 0):
break
if (f(element)):
ret_lst.append(element)
n = n - 1
return ret_lst
print(take_first_n([1,2,3,4,5],lambda x: x % 2 == 0,2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment