

{"id":123245,"date":"2023-11-02T16:57:49","date_gmt":"2023-11-02T11:27:49","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=123245"},"modified":"2024-02-28T11:44:16","modified_gmt":"2024-02-28T06:14:16","slug":"python-program-to-use-filter-function","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/python-program-to-use-filter-function\/","title":{"rendered":"Python Program to Use Filter() Function"},"content":{"rendered":"<p>In this article, we will explore a Python program that utilizes the filter function to selectively include elements from a list based on specific criteria defined by user-defined functions. The program focuses on filtering elements from a list based on whether they are prime numbers. This example demonstrates how the filter function can be employed to create a new list containing only elements that meet a specific condition.<\/p>\n<h2>Prerequisites<\/h2>\n<ul>\n<li>Familiarity with Python functions.<\/li>\n<li>Understanding of conditional statements.<\/li>\n<li>Knowledge of the filter function in Python.<\/li>\n<\/ul>\n<h3>Topic Explanation<\/h3>\n<p>This program introduces two essential functions: &#8216;even_odd(n)&#8217; for verifying number parity and &#8216;prime_no(n)&#8217; for identifying prime numbers. It begins by creating a list named &#8216;mylist,&#8217; populated with numerical values. The program then employs the &#8216;filter&#8217; function in conjunction with &#8216;prime_no&#8217; to generate a new list, &#8216;mylist1,&#8217; comprising solely of prime numbers extracted from the initial list.<\/p>\n<p>By combining these functions, the program provides a clear illustration of how to employ Python&#8217;s &#8216;filter&#8217; function effectively for extracting specific elements from a list. Readers can grasp the practical application of &#8216;filter&#8217; in isolating prime numbers, further enhancing their understanding of Python&#8217;s functional programming capabilities and list manipulation techniques.<\/p>\n<h4>Program<\/h4>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\"># Below Function check if a n is even or odd\r\ndef checkEvenOdd(n):\r\n    return n % 2 == 0\r\n\r\n#Below Function check if a n is prime or not\r\ndef prime_no(n):\r\n    i = 2\r\n    f = 0\r\n    while i &lt; n:\r\n        if n % i == 0:\r\n            f = 1\r\n        i += 1\r\n    return f == 0\r\n\r\n# List of numbers\r\nmylist = [10, 7, 8, 12, 9, 33, 45, 13, 15, 17]\r\n\r\n# Filtering prime numbers from the list\r\nmylist1 = list(filter(prime_no, mylist))\r\n\r\n# Printing the list of prime numbers\r\nprint(mylist1)<\/pre>\n<div class=\"df-code-out\"><strong>Output:<\/strong><br \/>\n[7, 13, 17]<\/div>\n<h4>Code Explanation:<\/h4>\n<ul>\n<li>Defines a function even_odd(n) that returns True if n is even, False if n is odd<\/li>\n<li>Defines a function prime_no(n) that returns True if n is prime, False otherwise<\/li>\n<li>Works by checking if n is divisible by any number between 2 and n-1<\/li>\n<li>Creates a list mylist with elements 10, 7, 8, 12, 9, 33, 45, 13, 15, 17<\/li>\n<li>Passes the prime_no function and mylist to filter()<\/li>\n<li>filter() will apply the prime_no function to each element in mylist and filter values based on true\/false returned<\/li>\n<li>Only elements for which prime_no() returns True will be present in the filtered list<\/li>\n<li>Converts the filter object returned to list using list() and stores in mylist1<\/li>\n<li>So mylist1 will contain only prime numbers from the given list<\/li>\n<li>Prints mylist1 which contains [7, 13, 17]<\/li>\n<\/ul>\n<h3>Summary<\/h3>\n<p>In summary, this Python program offers a practical example of using the filter function to selectively include elements in a new list based on a specific condition defined by user-defined functions. It demonstrates how this function can be a powerful tool for data manipulation in Python, allowing developers to create refined lists based on specific criteria. Understanding the filter function expands one&#8217;s ability to work with data structures and apply custom conditions for filtering elements.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we will explore a Python program that utilizes the filter function to selectively include elements from a list based on specific criteria defined by user-defined functions. The program focuses on filtering&#46;&#46;&#46;<\/p>\n","protected":false},"author":581,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[28666,10333,28626,28665],"class_list":["post-123245","post","type-post","status-publish","format-standard","hentry","category-python","tag-filter-function-in-python","tag-python","tag-python-practical","tag-python-program-to-use-filter-function"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python Program to Use Filter() Function - DataFlair<\/title>\n<meta name=\"description\" content=\"This Python program using the filter function to selectively include elements in a new list based on a specific condition defined by user-defined functions.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/python-program-to-use-filter-function\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Program to Use Filter() Function - DataFlair\" \/>\n<meta property=\"og:description\" content=\"This Python program using the filter function to selectively include elements in a new list based on a specific condition defined by user-defined functions.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/python-program-to-use-filter-function\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-11-02T11:27:49+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-02-28T06:14:16+00:00\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Program to Use Filter() Function - DataFlair","description":"This Python program using the filter function to selectively include elements in a new list based on a specific condition defined by user-defined functions.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/python-program-to-use-filter-function\/","og_locale":"en_US","og_type":"article","og_title":"Python Program to Use Filter() Function - DataFlair","og_description":"This Python program using the filter function to selectively include elements in a new list based on a specific condition defined by user-defined functions.","og_url":"https:\/\/data-flair.training\/blogs\/python-program-to-use-filter-function\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2023-11-02T11:27:49+00:00","article_modified_time":"2024-02-28T06:14:16+00:00","author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/python-program-to-use-filter-function\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/python-program-to-use-filter-function\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/c187795dc82ab948373cca526df7c445"},"headline":"Python Program to Use Filter() Function","datePublished":"2023-11-02T11:27:49+00:00","dateModified":"2024-02-28T06:14:16+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/python-program-to-use-filter-function\/"},"wordCount":411,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"keywords":["filter function in python","Python","python practical","python program to use filter function"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/python-program-to-use-filter-function\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/python-program-to-use-filter-function\/","url":"https:\/\/data-flair.training\/blogs\/python-program-to-use-filter-function\/","name":"Python Program to Use Filter() Function - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"datePublished":"2023-11-02T11:27:49+00:00","dateModified":"2024-02-28T06:14:16+00:00","description":"This Python program using the filter function to selectively include elements in a new list based on a specific condition defined by user-defined functions.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/python-program-to-use-filter-function\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/python-program-to-use-filter-function\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/python-program-to-use-filter-function\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Python Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/python\/"},{"@type":"ListItem","position":3,"name":"Python Program to Use Filter() Function"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/c187795dc82ab948373cca526df7c445","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/2302ebc438084d2f1f993edc1996a0aae01332e81f3227cba8df0c48ec010ca4?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/2302ebc438084d2f1f993edc1996a0aae01332e81f3227cba8df0c48ec010ca4?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/2302ebc438084d2f1f993edc1996a0aae01332e81f3227cba8df0c48ec010ca4?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam6\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/123245","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/581"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=123245"}],"version-history":[{"count":3,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/123245\/revisions"}],"predecessor-version":[{"id":134133,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/123245\/revisions\/134133"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=123245"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=123245"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=123245"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}