Friday, December 27, 2024
Google search engine
HomeLanguagesPython – Itertools.filterfalse()

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:




# 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)))  


Output:

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)))


Output:

[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))) 


Output:

[5, 7]

RELATED ARTICLES

Most Popular

Recent Comments