{"id":4791,"date":"2021-05-01T15:02:28","date_gmt":"2021-05-01T09:32:28","guid":{"rendered":"https:\/\/python-programs.com\/?p=4791"},"modified":"2021-11-22T18:42:58","modified_gmt":"2021-11-22T13:12:58","slug":"python-filter-function-tutorial-and-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-filter-function-tutorial-and-examples\/","title":{"rendered":"Python : filter() function | Tutorial and Examples"},"content":{"rendered":"
This article is about using This where,<\/p>\n Actually filter() function iterates over all elemnts of the sequence and for each element the given callback function is called.<\/p>\n In the above example we had created one separate function as In this we will pass an lambda function rather passing a separate function in So, let’s see an example of it.<\/p>\n Now, let’s take example how to filter a character in a string and remove that character in the string.<\/p>\n Suppose we have an array. So, let’s see it how we can filter the elements from an array.<\/p>\n filter() function in Python This article is about using filter() with lambda. filter() function : This filter() function is used to filter some content from a given sequence where the sequence may be a list, tuple or string etc. Syntax : filter(function, iterable) where, function refers to a function that accepts an argument and returns …<\/p>\nfilter()<\/code> with lambda.<\/p>\n
filter() function :<\/h3>\n
filter()<\/code> function is used to filter some content from a given sequence where the sequence may be a list, tuple or string etc.<\/p>\n
Syntax : filter<\/span>(<\/span>function, iterable<\/span>)<\/span><\/pre>\n
\n
How it works ?<\/h4>\n
\n
Filter a list of strings in Python using filter() :<\/h3>\n
#Program :\r\n\r\n# A list containg string as elemnt\r\nwords = ['ant', 'bat', 'dog', 'eye', 'ink', 'job']\r\n\r\n# function that filters words that begin with vowel\r\ndef filter_vowels(words):\r\n vowels = ['ant', 'eye', 'ink']\r\n\r\n if(words in vowels):\r\n return True\r\n else:\r\n return False\r\n\r\nfiltered_vowels = filter(filter_vowels, words)\r\n\r\n\r\n# Print words that start with vowel\r\nprint('The filtered words are:')\r\nfor vowel in filtered_vowels:\r\nprint(vowel)<\/pre>\n
Output :\r\nant\r\neye\r\nink<\/pre>\n
filtered_vowels<\/code> and we passed it to
filter()<\/code> function.<\/p>\n
Using filter() with Lambda function :<\/h3>\n
filter()<\/code> function. And the condition is to select words whose length is 6.<\/p>\n
#Program :\r\n\r\n# A list containg string as elemnt\r\nwords = ['ant', 'basket', 'dog', 'table', 'ink', 'school']\r\n\r\n#It will return string whose length is 6\r\nfiltered_vowels = list(filter(lambda x : len(x) == 6 , words))\r\n\r\n\r\n# Print words that start with vowel\r\nprint('The filtered words are:')\r\nfor vowel in filtered_vowels:\r\n print(vowel)<\/pre>\n
Output :\r\nThe filtered words are:\r\nbasket\r\nschool<\/pre>\n
Filter characters from a string in Python using filter() :<\/h3>\n
#Program :\r\n\r\n# A list containg string as elemnt\r\nstr_sample = \"Hello, you are studying from Btech Geeks.\"\r\n\r\n#It will return particular character in the string\r\n#then those chharacters are removed from the string\r\nfilteredChars = ''.join((filter(lambda x: x not in ['e', 's'], str_sample)))\r\n\r\n\r\n# Print the new string \r\nprint('Filtered Characters : ', filteredChars)<\/pre>\n
Output : \r\nHllo, you ar tudying from Btch Gk.<\/pre>\n
Filter an array in Python using filter() :<\/h3>\n
#Program :\r\n\r\n# Two arrays \r\nsample_array1 = [1,3,4,5,21,33,45,66,77,88,99,5,3,32,55,66,77,22,3,4,5]\r\nsample_array2 = [5,3,66]\r\n\r\n#It will return a new array\r\n#It will filter\r\n#if array2 elemnts are present in array1 then that element will not be removed\r\nfiltered_Array = list(filter(lambda x : x not in sample_array2, sample_array1))\r\nprint('Filtered Array : ', filtered_Array)<\/pre>\n
Output :\r\n\r\n[1, 4, 21, 33, 45, 77, 88, 99, 32, 55, 77, 22 ,4]<\/pre>\n","protected":false},"excerpt":{"rendered":"