Change language

Python filter () function

|

The Python filter () function is used to filter the elements of an iteration (sequence) using a predicate that checks each element during the iteration.

Predicate &it is a function that always returns True or False . We cannot use a generic function with filter () , because it will return all elements only when a matching condition is met. This means that the filter function must always return a boolean, and therefore the filter function is a predicate.

Basic filter format

Since this is a function that works with an iterable Python, iteration is one of the parameters. And since it checks the predicate for each element, the function is also another required parameter.



AND since it filters elements from the sequence, it must also return an iteration that only consists of elements that satisfy the filtering function.

But in this case, since As we work with objects, Python returns us the filter object as an iteration, which will be convenient for converting to other types using methods such as list () and dict () .

Simple, right? Let’s see how we apply this and create working programs using filter ()

Format: filter_object = filter (predicate, iterable)



Here’s a very simple example that filters a list with a function that checks if a number is even or odd.

 a = [1, 2 , 3, 4, 5] # We filter using a lambda function predicate. # Thi s predicate returns true # only if the number is even. filter_obj_even = filter (lambda x: x% 2 == 0, a) print (type (filter_obj_even)) # Convert to a list using list () print (’Even numbers:’, list (filter_obj_even)) # We can also use define the predicate using def () def odd (num): return (num% 2)! = 0 filter_obj_odd = filter (odd, a) print (’Odd numbers:’, list (filter_obj_odd)) 

Output

 "class ’filter’" Even numbers: [2, 4] Odd numbers: [1, 3, 5] 

Note that we can get the individual elements of the filter object by iterating over it since it is iterable:

 for item in filter_obj_odd: print (item) 

Output

 1 3 5 

filter () and None

We can also use None as a predicate with filter () None returns True if the object is Boolean True , and False otherwise.

This means that objects such as 0 , None , ’’ , [] etc., All are filtered by the None predicate because they are empty element objects.

 a = [0, 1, ’Hello’ , ’’, [], [1,2,3], 0.1, 0.0] print (list (filter (None, a))) 

Output

 
 [1, ’Hello’, [1, 2, 3], 0.1] 



Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News


Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method