Python - Itertools.filterfalse()
Last Updated :
11 Jun, 2020
Improve
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
This iterator prints only values that return false for the passed function.
Syntax:
Python3 1==
Output:
Python3 1==
Output:
Python3 1==
Output:
filterfalse() function
This iterator prints only values that return false for the passed function.
Syntax:
filterfalse(function or None, sequence) --> filterfalse objectParameter: 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:
# Python program to demonstrate
# the working of filterfalse
import itertools
from itertools import filterfalse
# function is a None
for i in filterfalse(None, range(20)):
print(i)
li = [2, 4, 5, 7, 8, 10, 20]
# Slicing the list
print(list(itertools.filterfalse(None, li)))
0 []Example 2:
# Python program to demonstrate
# the working of filterfalse
import itertools
from itertools import filterfalse
def filterfalse(y):
return (y > 5)
li = [2, 4, 5, 7, 8, 10, 20]
# Slicing the list
print(list(itertools.filterfalse(filterfalse, li)))
[2, 4, 5]Example 3:
# Python program to demonstrate
# the working of filterfalse
import itertools
from itertools import filterfalse
li = [2, 4, 5, 7, 8, 10, 20]
# Slicing the list
print (list(itertools.filterfalse(lambda x : x % 2 == 0, li)))
[5, 7]