{"id":24815,"date":"2021-11-02T09:48:05","date_gmt":"2021-11-02T04:18:05","guid":{"rendered":"https:\/\/python-programs.com\/?p=24815"},"modified":"2021-11-05T20:37:04","modified_gmt":"2021-11-05T15:07:04","slug":"python-filter-function-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-filter-function-with-examples\/","title":{"rendered":"Python filter() Function with Examples"},"content":{"rendered":"
In the previous article, we have discussed Python enumerate() Function with Examples<\/a> The filter() function returns an iterator in which the items are filtered through a function to determine whether or not the item is accepted.<\/p>\n Syntax:<\/strong><\/p>\n Parameter Values:<\/strong><\/p>\n function:<\/strong> It is a function that will be executed for each item in the iterable.<\/p>\n iterable:<\/strong> The iterable that will be filtered.<\/p>\n Return Value:<\/strong><\/p>\n The iterator is returned by the filter() function.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/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 Inside the filter, we have directly passed a lambda function ().<\/p>\n For Odd numbers, our lambda function returns True. As a result, the filter() function returns an iterator that only contains Odd numbers.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Fed up with searching various pages for the list of Python Built in Functions<\/a>? Look at the tutorial linked here and explore all coding samples of built-in functions of python.<\/p>\n In the previous article, we have discussed Python enumerate() Function with Examples filter() Function in Python: The filter() function returns an iterator in which the items are filtered through a function to determine whether or not the item is accepted. Syntax: filter(function, iterable) Parameter Values: function: It is a function that will be executed for …<\/p>\n
\nfilter() Function in Python:<\/strong><\/p>\nfilter(function, iterable)<\/pre>\n
Given List = [3, 10, 12, 13, 20, 7, 1, 16]<\/pre>\n
The Even numbers in a given list :\r\n10\r\n12\r\n20\r\n16<\/pre>\n
Given List = [4, 6, 7, 9, 10, 12, 11]<\/pre>\n
The Even numbers in a given list :\r\n4\r\n6\r\n10\r\n12<\/pre>\n
filter() Function with Examples in Python<\/h2>\n
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
\n
# Give the list as static input and store it in a variable.\r\ngvn_lst = [3, 10, 12, 13, 20, 7, 1, 16]\r\n\r\n# Create a function say Even_numbers which accepts a number as an argument and returns\r\n# true if the argument is even number else it returns false.\r\n\r\n\r\ndef Even_numbers(numb):\r\n # Check if the given number is even using the if conditional statement.\r\n if numb % 2 == 0:\r\n # If it is true, then return True.\r\n return True\r\n # Else return False.\r\n else:\r\n return False\r\n\r\n\r\n# Pass the above Function(Even_numbers),\r\n# given list as arguments to the filter function that\r\n# returns an iterator in which the items are filtered\r\n# through a function to determine whether or not the item is accepted.\r\n# Store it in another variable.\r\nevn_numbrs = filter(Even_numbers, gvn_lst)\r\nprint(\"The Even numbers in a given list :\")\r\n# Loop in the above result using the for loop.\r\nfor n in evn_numbrs:\r\n # Print the iterator value of the for loop.\r\n print(n)\r\n<\/pre>\n
The Even numbers in a given list :\r\n10\r\n12\r\n20\r\n16<\/pre>\n
Using the Lambda Function Within a filter ()<\/h5>\n
gvn_lst = [1, 2, 3, 4, 5, 6, 7, 8, 9]\r\n\r\n# The lambda function returns True for odd numbers\r\noddnumbrs_itrtor = filter(lambda n: (n % 2 != 0), gvn_lst)\r\n\r\n# converting to list\r\nodd_numbrs = list(oddnumbrs_itrtor)\r\nprint(\"The Odd numbers in a given list:\")\r\nprint(odd_numbrs)\r\n<\/pre>\n
The Odd numbers in a given list:\r\n[1, 3, 5, 7, 9]<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
\n
# Give the list as user input using list(),int(),split() and map() functions\r\n# and 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\r\n# Create a function say Even_numbers which accepts a number as an argument and returns\r\n# true if the argument is even number else it returns false.\r\n\r\n\r\ndef Even_numbers(numb):\r\n # Check if the given number is even using the if conditional statement.\r\n if numb % 2 == 0:\r\n # If it is true, then return True.\r\n return True\r\n # Else return False.\r\n else:\r\n return False\r\n\r\n\r\n# Pass the above Function(Even_numbers),\r\n# given list as arguments to the filter function that\r\n# returns an iterator in which the items are filtered\r\n# through a function to determine whether or not the item is accepted.\r\n# Store it in another variable.\r\nevn_numbrs = filter(Even_numbers, gvn_lst)\r\nprint(\"The Even numbers in a given list :\")\r\n# Loop in the above result using the for loop.\r\nfor n in evn_numbrs:\r\n # Print the iterator value of the for loop.\r\n print(n)\r\n<\/pre>\n
Enter some random List Elements separated by spaces = 4 6 7 9 10 12 11\r\nThe Even numbers in a given list :\r\n4\r\n6\r\n10\r\n12<\/pre>\n
\n