Python – Itertools.filterfalse()
In Python, Itertools is the inbuilt module that allows us to handle the iterators in an efficient way. They make iterating through the iterables like lists and strings very easily. One such itertools function is filterfalse().
Note: For more information, refer to Python Itertools
filterfalse() function
This iterator prints only values that return false for the passed function.
Syntax:
filterfalse(function or None, sequence) --> filterfalse object
Parameter: This method contains two arguments, the first argument is function or None and the second argument is list of integer.
Return Value: This method returns the only values that return false for the passed function.
Example 1:
import itertools
from itertools import filterfalse
for i in filterfalse( None , range ( 20 )):
print (i)
li = [ 2 , 4 , 5 , 7 , 8 , 10 , 20 ]
print ( list (itertools.filterfalse( None , li)))
|
Output:
0
[]
Example 2:
import itertools
from itertools import filterfalse
def filterfalse(y):
return (y > 5 )
li = [ 2 , 4 , 5 , 7 , 8 , 10 , 20 ]
print ( list (itertools.filterfalse(filterfalse, li)))
|
Output:
[2, 4, 5]
Example 3:
import itertools
from itertools import filterfalse
li = [ 2 , 4 , 5 , 7 , 8 , 10 , 20 ]
print ( list (itertools.filterfalse( lambda x : x % 2 = = 0 , li)))
|
Output:
[5, 7]
Last Updated :
11 Jun, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...