Nigel Redding
Nigel Redding

Reputation: 1

Search through list and do based on predicate in python

Suppose we have a list of integers L in python, and a function F which takes in an integer and returns a boolean. I have the following code:

for i in L:
   if F(i):
      do_whatever(i)

is there a way of doing this in one line in python, or rather, a more pythonic approach?

Upvotes: 0

Views: 77

Answers (1)

kezzos
kezzos

Reputation: 3221

Using the built-in python functions:

map(do_whatever, filter(F, L)) 

Upvotes: 1

Related Questions