avatarIndhumathy Chelliah

Summarize

Filter() vs Filterfalse() in Python

Let’s learn about the differences between filter( ) and itertools.filterfalse( ) in python.

Photo by Ketut Subiyanto from Pexels

filter vs itertools.filterfalse

filter

Construct an iterator from those elements of iterable for which function returns true. iterable may be either a sequence, a container which supports iteration, or an iterator. If function is None, the identity function is assumed, that is, all elements of iterable that are false are removed.

Note that filter(function, iterable) is equivalent to the generator expression (item for item in iterable if function(item)) if function is not None and (item for item in iterable if item) if function is None. -python docs

filter(function, iterable)

iterable — can be sequences like list, tuple, or container or an iterator.

Return type is a filter object which is an iterator.

We can access the filter object which is an iterator using below mentioned ways.

  • We can convert the filter object to sequence objects like list using list() constructor, tuple using tuple() constructor.
  • We can iterate through the filter object using for loop also
  • We can access the element in the filter object using the next() function also.
Image Source:Author

Example 1:Using user-defined function in filter()

  • List num1 is given as an iterable.
  • function even()is defined to find the even numbers.
  • filter() will return a filter object which is an iterator.
  • Converting the filter object to list using list() constructor.
def even(x):
    if x%2==0:
        return True

num1=[1,2,3,4,5,6,7,8,9,10]
even=filter(even,num1)
print (even)#Output:<filter object at 0x00E5E4D8>
print (type(even))#Output:<class 'filter'>
print (list(even))#Output:[2, 4, 6, 8, 10]

Example 2:Using lambda function in filter()

Filtering values greater than 10 from the dictionary.

d={'a':100,'b':40,'c':2}
d1=filter(lambda x:x>10,d.values())
print (d1)#Output:<filter object at 0x00E5E4D8>
print (type(d1))#Output:<class 'filter'>
print (list(d1))#Output:[100, 40]

Example 3: Using None in filter()

If function is None, the identity function is assumed, that is, all elements of iterable that are false are removed.

num1=[1,0,2,3,{},False]
d1=filter(None,num1)
print (d1)#Output:<filter object at 0x00E5E4D8>
print (type(d1))#Output:<class 'filter'>
print (list(d1))#Output:[1, 2, 3]

Example 4: Iterating through filter object using for loop

  • Filtering items in the list which start with “r”
  • Iterating through filter object using for loop
num1=["red","rain","region",'blue','purple']
d1=filter(lambda x:x.startswith("r"),num1)
print (d1)#Output:<filter object at 0x00E5E4D8>
print (type(d1))#Output:<class 'filter'>
for i in d1:
    print (i)
'''    
#Output:
red
rain
region
'''

Example 5: Iterating through filter object using next() function

  • We can access the next element in an iterator using the next() function.

Refer to my story iterators.

t1=(1,2,3,4,5)
d1=filter(lambda x:x%2!=0,t1)
print (d1)#Output:<filter object at 0x00E5E4D8>
print (type(d1))#Output:<class 'filter'>
print (next(d1))#Output:1
print (next(d1))#Output:3
print (next(d1))#Output:5

itertools.filterfalse

itertools.filterfalse(predicate, iterable)

Make an iterator that filters elements from iterable returning only those for which the predicate is False. If predicate is None, return the items that are false. -python docs

Image Source:Author

Example 1:Using lambda function in filterfalse()

  • Import itertools module
  • Define the lambda function to filter the elements greater than 4.
  • Pass lambda function and iterable (list l1) to itertools.filterfalse() function.
  • filterfalse() will return the elements less than 4. It will filter the elements from the iterable which returns False for the given function.
  • Return type is an iterator. Converting to list using list() constructor.
import itertools
#iterator will filter the elements from the iterable which returns False for the given function
num1=itertools.filterfalse(lambda x:x>4,[1,2,3,4,5,6,7,8,9])
print (num1)#Output:<itertools.filterfalse object at 0x0083E658>
print (list(num1))#Output:[1, 2, 3,4]

Example 2:Using user-defined function in filterfalse()

import itertools
def even(x):
    if x%2==0:
        return x
num1=[1,2,3,4,5,6,7,8,9,10]
num2=itertools.filterfalse(even,num1)

print (list(num2))
#Output:[1, 3, 5, 7, 9]

Example 3: Using None in filterfalse().

#If predicate is None, returns the items that are False.
num5=itertools.filterfalse(None,[0,1,2,3,4,5])
print (list(num5))#Output:[0]

Example 4: Iterating through filterfalse object using for loop

from itertools import filterfalse
num1=["red","rain","region",'blue','purple']

d1=filterfalse(lambda x:x.startswith("r"),num1)
print (d1)#Output:<itertools.filterfalse object at 0x0148E4D8>

print (type(d1))#Output:<class 'itertools.filterfalse'>
for i in d1:
    print (i)
'''    
#Output:
blue
purple
'''

Example 5: Iterating through filterfalse object using next() function

  • We can access the next element in an iterator using the next() function.
import itertools
t1=(1,2,3,4,5)
d1=itertools.filterfalse(lambda x:x%2!=0,t1)
print (d1)#Output:<itertools.filterfalse object at 0x0177E658>
print (type(d1))#Output:<class 'itertools.filterfalse'>
print (next(d1))#Output:2
print (next(d1))#Output:4

Conclusion:

  • filter(): Construct an iterator from the elements of iterable for which function returns True.
  • filterfalse(): Construct an iterator from the elements of iterable for which function returns False.
  • filter() — built-in function
  • filterfalse()-have to import the itertools module.

My other blog links

map() vs starmap()

reduce vs accumulate()

Resources(Python Documentation):

filter-python documentation

itertools.filterfalse-python documentation.

Watch this space for more articles on Python and DataScience. If you like to read more of my tutorials, follow me on Medium, LinkedIn, Twitter.

Thanks for reading!

Python3
Python
Artificial Intelligence
Women In Tech
Technology
Recommended from ReadMedium