{"id":25590,"date":"2021-11-23T08:55:13","date_gmt":"2021-11-23T03:25:13","guid":{"rendered":"https:\/\/python-programs.com\/?p=25590"},"modified":"2021-11-23T08:55:13","modified_gmt":"2021-11-23T03:25:13","slug":"python-itertools-filterfalse-function-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-itertools-filterfalse-function-with-examples\/","title":{"rendered":"Python Itertools.filterfalse() Function with Examples"},"content":{"rendered":"
Itertools Module:<\/strong><\/p>\n 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.<\/p>\n Itertools.filterfalse() Function:<\/strong><\/p>\n This iterator prints only values for the passed function that return false.<\/p>\n Syntax:<\/strong><\/p>\n Parameter Values:<\/strong><\/p>\n filterfalse() method takes two arguments: function or None as the first and a list of integers as the second argument.<\/p>\n Return Value:<\/strong><\/p>\n This method returns the only values for the passed function that return false.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n 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 …<\/p>\nfilterfalse(function or None, sequence)<\/pre>\n
Itertools.filterfalse() Function with Examples in Python<\/h2>\n
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
\u00a0With giving Function as None<\/h5>\n
\n
# Import itertools module using the import keyword\r\nimport itertools\r\n# Import filterfalse() function from itertools using the import keyword.\r\nfrom itertools import filterfalse\r\n\r\n\r\n# Loop using the filterfalse() function and for loop by passing the arguments\r\n# as function(None) and range.\r\nfor itr in filterfalse(None, range(15)):\r\n print(itr)\r\n\r\n# Give the list as static input and store it in a variable.\r\ngvn_lst = [1, 4, 6, 3, 8]\r\n# Use the filterfalse() function by passing the arguments as given function(None)\r\n# and list for slicing the given list.\r\n# Store it in another variable.\r\nrslt = itertools.filterfalse(None, gvn_lst)\r\n# Convert the above result into a list using the list() function and store it in\r\n# another variable.\r\nrslt_lst = list(rslt)\r\n# Print the above result.\r\nprint(rslt_lst)\r\n<\/pre>\n
0\r\n[]<\/pre>\n
With giving Function :<\/h5>\n
\n
# Import itertools module using the import keyword\r\nimport itertools\r\n# Import filterfalse() function from itertools using the import keyword.\r\nfrom itertools import filterfalse\r\n\r\n# Create a function say Demo_function which accepts the number as argument and\r\n# returns True if the number is greater than 7.\r\n\r\n\r\ndef Demo_function(num):\r\n # Return True if the given argument is greater than 7\r\n return (num > 7)\r\n\r\n\r\n# Give the list as static input and store it in a variable.\r\ngvn_lst = [1, 3, 4, 8, 11]\r\n# Use the filterfalse() function by passing the arguments as above function\r\n# and given list for slicing the given list.\r\n# Store it in another variable.\r\nrslt = itertools.filterfalse(Demo_function, gvn_lst)\r\n# Convert the above result into a list using the list() function and store it in\r\n# another variable.\r\nrslt_lst = list(rslt)\r\n# Print the above result.\r\nprint(rslt_lst)\r\n<\/pre>\n
[1, 3, 4]<\/pre>\n
Using lambda() Function<\/h3>\n
# Import itertools module using the import keyword\r\nimport itertools\r\n# Import filterfalse() function from itertools using the import keyword.\r\nfrom itertools import filterfalse\r\n# Give the list as static input and store it in a variable.\r\ngvn_lst = [1, 2, 3, 4, 7, 8, 6, 9]\r\n\r\n# Use the filterfalse() function for slicing the given list\r\nprint(list(itertools.filterfalse(lambda num: num % 2 != 0, gvn_lst)))\r\n<\/pre>\n
[2, 4, 8, 6]<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
\n
# Import itertools module using the import keyword\r\nimport itertools\r\n# Import filterfalse() function from itertools using the import keyword.\r\nfrom itertools import filterfalse\r\n\r\n# Create a function say Demo_function which accepts the number as argument and\r\n# returns True if the number is greater than 7.\r\n\r\n\r\ndef Demo_function(num):\r\n # Return True if the given argument is greater than 7\r\n return (num > 7)\r\n\r\n\r\n# Give the list as user input using list(),map(),input(),and split() functions.\r\n# Store it in a variable.\r\ngvn_lst = list(map(int, input(\r\n 'Enter some random List Elements separated by spaces = ').split()))\r\n# Use the filterfalse() function by passing the arguments as above function\r\n# and given list for slicing the given list.\r\n# Store it in another variable.\r\nrslt = itertools.filterfalse(Demo_function, gvn_lst)\r\n# Convert the above result into a list using the list() function and store it in\r\n# another variable.\r\nrslt_lst = list(rslt)\r\n# Print the above result.\r\nprint(rslt_lst)\r\n<\/pre>\n
Enter some random List Elements separated by spaces = -2 -3 2 4 6 10 13\r\n[-2, -3, 2, 4, 6]<\/pre>\n","protected":false},"excerpt":{"rendered":"