{"id":12885,"date":"2021-12-09T14:43:39","date_gmt":"2021-12-09T14:43:39","guid":{"rendered":"https:\/\/linuxways.net\/?p=12885"},"modified":"2021-12-16T08:04:41","modified_gmt":"2021-12-16T08:04:41","slug":"python-filter-function","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/scripting\/python-filter-function\/","title":{"rendered":"Python filter() Function"},"content":{"rendered":"<h2>Introduction<\/h2>\n<p>The filter() function helps the user to filter a sequence of elements according to the given condition to check if it is true or not. This function helps to filter elements in the sequence faster and more accurately. And the filtered-out elements are sorted in the original order.<\/p>\n<p>Now we&#8217;re gonna guide you about using the filter() function in Python. Hope you understand.<\/p>\n<h2>Example<\/h2>\n<p>Filter numbers less than or equal to 6:<\/p>\n<pre>num = [3, 7, 5, 9, 1, 2, 8, 6]\r\n\r\ndef func(x):\r\n\r\nif x &gt; 6:\r\n\r\nreturn False\r\n\r\nelse:\r\n\r\nreturn True\r\n\r\nnumber = filter(func, num)\r\n\r\nfor x in number:\r\n\r\nprint(x)<\/pre>\n<p>Output:<br \/>\n<strong>3<\/strong><\/p>\n<p><strong>5<\/strong><\/p>\n<p><strong>1<\/strong><\/p>\n<p><strong>2<\/strong><\/p>\n<p><strong>6<\/strong><\/p>\n<h2>Definition<\/h2>\n<p>The filter() function prints the result according to the accepted condition.<\/p>\n<h2>The syntax<\/h2>\n<pre>filter(function, iterable)<\/pre>\n<p><strong>Parameter Values<\/strong>:<\/p>\n<p>function: function to check element<\/p>\n<p>iterable: sequence to be filtered<\/p>\n<h2>More examples<\/h2>\n<p><strong>Example 1<\/strong>: Filter even numbers<\/p>\n<pre>num = [3, 7, 5, 9, 1, 2, 8, 6]\r\n\r\ndef even(x):\r\n\r\nif x % 2 == 0:\r\n\r\nreturn True\r\n\r\nelse:\r\n\r\nreturn False\r\n\r\nnumber = filter(even, num)\r\n\r\nfor x in number:\r\n\r\nprint(x)<\/pre>\n<p>Output:<br \/>\n<strong>2<\/strong><\/p>\n<p><strong>8<\/strong><\/p>\n<p><strong>6<\/strong><\/p>\n<p><strong>Example 2<\/strong>: filters vowels<\/p>\n<p># filters vowels<\/p>\n<pre>def vowel(x):\r\n\r\nvowels = ['a', 'e', 'i', 'o', 'u']\r\n\r\nif (x in vowels):\r\n\r\nreturn True\r\n\r\nelse:\r\n\r\nreturn False\r\n\r\n# sequence\r\n\r\nletters = ['a', 'g', 'h', 'i', 'k']\r\n\r\n# using filter()\r\n\r\nfiltered = filter(vowel, letters)\r\n\r\nfor x in filtered:\r\n\r\nprint(x)<\/pre>\n<p>Output:<br \/>\n<strong>a<\/strong><\/p>\n<p><strong>i<\/strong><\/p>\n<h2>Conclusion<\/h2>\n<p>And we guided you on how to use the filter() function in Python.<\/p>\n<p>Thank you for checking it out!<\/p>","protected":false},"excerpt":{"rendered":"<p>Introduction The filter() function helps the user to filter a sequence of elements according to the given condition to check if it is true or not. This function&hellip;<\/p>","protected":false},"author":1,"featured_media":13010,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[168],"tags":[755,10],"class_list":["post-12885","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-filter","tag-python"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/12885","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/comments?post=12885"}],"version-history":[{"count":1,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/12885\/revisions"}],"predecessor-version":[{"id":13009,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/12885\/revisions\/13009"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/13010"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=12885"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=12885"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=12885"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}