Skip to content

Instantly share code, notes, and snippets.

@IndhumathyChelliah
Last active July 6, 2020 00:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IndhumathyChelliah/34c438ed316a906baa1f5a48cb0c5957 to your computer and use it in GitHub Desktop.
Save IndhumathyChelliah/34c438ed316a906baa1f5a48cb0c5957 to your computer and use it in GitHub Desktop.
import itertools
#iterator will filter the elements from the iterable which returns False for the given function
l1=itertools.filterfalse(lambda x:x>4,[1,2,3,4,5,6,7,8,9])
print (l1)#Output:<itertools.filterfalse object at 0x0083E658>
print (list(l1))#Output:[1, 2, 3,4]
#filter() function will filter the elements from the iterable which returns True for the given function
l4=filter(lambda x:x>4,[1,2,3,4,5,6,7,8,9])
print (list(l4))#Output:[5, 6, 7, 8, 9]
#iterator will filter the elements from the iterable which returns False for the given function
l2=itertools.filterfalse(lambda x:x%2==0,[2,4,6,8,10])
print (list(l2))#Output:[]
#filter() function will filter the elements from the iterable which returns True for the given function
l3=filter(lambda x:x%2==0,[2,4,6,8,10])
print (list(l3))#Output:[2, 4, 6, 8, 10]
#If predicate is None, returns the items that are False.
l5=itertools.filterfalse(None,[0,1,2,3,4,5])
print (list(l5))#Output:[0]
#If predicate is None,filter() function returns the items that are True.
l6=filter(None,[0,1,2,3,4])
print (list(l6))#Output:[1, 2, 3, 4]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment