Itertools Module:
Itertools is a Python module that contains a collection of functions for dealing with iterators. They make it very simple to iterate through iterables such as lists and strings.
Itertools.filterfalse() Function:
This iterator prints only values for the passed function that return false.
Syntax:
filterfalse(function or None, sequence)
Parameter Values:
filterfalse() method takes two arguments: function or None as the first and a list of integers as the second argument.
Return Value:
This method returns the only values for the passed function that return false.
Itertools.filterfalse() Function with Examples in Python
Method #1: Using Built-in Functions (Static Input)
 With giving Function as None
Approach:
- Import itertools module using the import keyword.
- Import filterfalse() function from itertools using the import keyword.
- Loop using the filterfalse() function and for loop by passing the arguments as function(None) and range.
- Give the list as static input and store it in a variable.
- Use the filterfalse() function by passing the arguments as given function(None) and list for slicing the given list.
- Store it in another variable.
- Convert the above result into a list using the list() function and store it in another variable.
- Print the above result.
- The Exit of the Program.
Below is the implementation:
# Import itertools module using the import keyword
import itertools
# Import filterfalse() function from itertools using the import keyword.
from itertools import filterfalse
# Loop using the filterfalse() function and for loop by passing the arguments
# as function(None) and range.
for itr in filterfalse(None, range(15)):
print(itr)
# Give the list as static input and store it in a variable.
gvn_lst = [1, 4, 6, 3, 8]
# Use the filterfalse() function by passing the arguments as given function(None)
# and list for slicing the given list.
# Store it in another variable.
rslt = itertools.filterfalse(None, gvn_lst)
# Convert the above result into a list using the list() function and store it in
# another variable.
rslt_lst = list(rslt)
# Print the above result.
print(rslt_lst)
Output:
0 []
With giving Function :
Approach:
- Import itertools module using the import keyword.
- Import filterfalse() function from itertools using the import keyword.
- Create a function say Demo_function which accepts the number as argument and returns True if the number is greater than 7.
- Inside the function, return True if the given argument is greater than 7.
- Give the list as static input and store it in a variable.
- Use the filterfalse() function by passing the arguments as above function and given list for slicing the given list.
- Store it in another variable.
- Convert the above result into a list using the list() function and store it in another variable.
- Print the above result.
- The Exit of the Program.
Below is the implementation:
# Import itertools module using the import keyword
import itertools
# Import filterfalse() function from itertools using the import keyword.
from itertools import filterfalse
# Create a function say Demo_function which accepts the number as argument and
# returns True if the number is greater than 7.
def Demo_function(num):
# Return True if the given argument is greater than 7
return (num > 7)
# Give the list as static input and store it in a variable.
gvn_lst = [1, 3, 4, 8, 11]
# Use the filterfalse() function by passing the arguments as above function
# and given list for slicing the given list.
# Store it in another variable.
rslt = itertools.filterfalse(Demo_function, gvn_lst)
# Convert the above result into a list using the list() function and store it in
# another variable.
rslt_lst = list(rslt)
# Print the above result.
print(rslt_lst)
Output:
[1, 3, 4]
Using lambda() Function
# Import itertools module using the import keyword import itertools # Import filterfalse() function from itertools using the import keyword. from itertools import filterfalse # Give the list as static input and store it in a variable. gvn_lst = [1, 2, 3, 4, 7, 8, 6, 9] # Use the filterfalse() function for slicing the given list print(list(itertools.filterfalse(lambda num: num % 2 != 0, gvn_lst)))
Output:
[2, 4, 8, 6]
Method #2: Using Built-in Functions (User Input)
Approach:
- Import itertools module using the import keyword.
- Import filterfalse() function from itertools using the import keyword.
- Create a function say Demo_function which accepts the number as argument and returns True if the number is greater than 7.
- Inside the function, return True if the given argument is greater than 7.
- Give the list as user input using list(),map(),input(),and split() functions.
- Store it in a variable.
- Use the filterfalse() function by passing the arguments as above function and given list for slicing the given list.
- Store it in another variable.
- Convert the above result into a list using the list() function and store it in another variable.
- Print the above result.
- The Exit of the Program.
Below is the implementation:
# Import itertools module using the import keyword
import itertools
# Import filterfalse() function from itertools using the import keyword.
from itertools import filterfalse
# Create a function say Demo_function which accepts the number as argument and
# returns True if the number is greater than 7.
def Demo_function(num):
# Return True if the given argument is greater than 7
return (num > 7)
# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvn_lst = list(map(int, input(
'Enter some random List Elements separated by spaces = ').split()))
# Use the filterfalse() function by passing the arguments as above function
# and given list for slicing the given list.
# Store it in another variable.
rslt = itertools.filterfalse(Demo_function, gvn_lst)
# Convert the above result into a list using the list() function and store it in
# another variable.
rslt_lst = list(rslt)
# Print the above result.
print(rslt_lst)
Output:
Enter some random List Elements separated by spaces = -2 -3 2 4 6 10 13 [-2, -3, 2, 4, 6]