Example filter() in python

PythonServer Side ProgrammingProgramming

The filter function in Python is used to fetch some selected elements from a iterable using certain conditions. In this article we will take a list and choose some elements from it by applying certain condition.


filter(function, iterable)
function: A Function to be run for each item in the iterable
iterable: The iterable to be filtered

In the below example we define a function which will divide a number with 2 to check for any reminder and then decide if the number is odd or even. This function is applied to a list using filter().


 Live Demo

listA = [15, 8, 21, 13, 32]
def findeven(x):
   if x %2 !=0:
      return False
      return True
evenum = filter(findeven, listA)
for x in evenum:


Running the above code gives us the following result −

Updated on 09-Jul-2020 13:23:49