

{"id":123247,"date":"2023-11-02T17:07:10","date_gmt":"2023-11-02T11:37:10","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=123247"},"modified":"2024-02-28T11:45:35","modified_gmt":"2024-02-28T06:15:35","slug":"python-program-on-filter-function-with-lambda-function","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/python-program-on-filter-function-with-lambda-function\/","title":{"rendered":"Python Program on Filter Function with Lambda Function"},"content":{"rendered":"<p>In this article, we will explore a Python program that utilizes the filter function with lambda expressions. Lambda expressions, also known as anonymous functions, provide a concise way to create small, one-line functions. The program focuses on using the filter function with lambda expressions to selectively include elements from a list and characters from a string based on specific conditions.<\/p>\n<h2>Prerequisites<\/h2>\n<ul>\n<li>Familiarity with lambda expressions.<\/li>\n<li>Understanding of the filter function in Python.<\/li>\n<li>Knowledge of basic list and string operations in Python.<\/li>\n<\/ul>\n<h3>Topic Explanation<\/h3>\n<p>This program starts by creating a list, &#8216;mylist,&#8217; comprised of numeric elements and a string, &#8216;mystring.&#8217; It effectively utilizes the &#8216;filter&#8217; function in combination with a lambda expression to establish &#8216;mylist1,&#8217; a new list exclusively housing even numbers extracted from the original list.<\/p>\n<p>Similarly, the program employs &#8216;filter&#8217; with a lambda expression to generate &#8216;str1,&#8217; a fresh list encompassing solely the vowels found within the initial string. This dual demonstration showcases the flexibility of the &#8216;filter&#8217; function in processing both numerical and textual data, enhancing readers&#8217; understanding of Python&#8217;s functional capabilities and list\/string manipulation.<\/p>\n<h4><strong>Program Code:<\/strong><\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\"># Filter function with lambda expression\r\n\r\n# List of integers\r\nmylist = [10, 7, 8, 12, 66, 23, 88, 67]\r\n\r\n# Filtering even numbers using lambda expression\r\nmylist1 = list(filter(lambda x: (x % 2 == 0), mylist))\r\n\r\n# Printing the filtered list of even numbers\r\nprint(mylist1)\r\n\r\n# String for vowel filtering\r\nmystring = \"Data Flair provide free course\"\r\n\r\n# Filtering vowels using lambda expression\r\nstr1 = list(filter(lambda v: (v == 'a' or v == 'e' or v == 'i' or v == 'o' or v == 'u'), mystring))\r\n\r\n# Printing the filtered list of vowels\r\nprint(str1)<\/pre>\n<div class=\"df-code-out\"><strong>Output:<\/strong><br \/>\n[10, 8, 12, 66, 88]<br \/>\n[&#8216;a&#8217;, &#8216;a&#8217;, &#8216;a&#8217;, &#8216;i&#8217;, &#8216;o&#8217;, &#8216;i&#8217;, &#8216;e&#8217;, &#8216;e&#8217;, &#8216;e&#8217;, &#8216;o&#8217;, &#8216;u&#8217;, &#8216;e&#8217;]<\/div>\n<h3>Code Explanation:<\/h3>\n<ul>\n<li><strong>mylist is a list containing the numbers:<\/strong> 10, 7, 8, 12, 66, 23, 88, 67<\/li>\n<li>A lambda function is defined that checks if a number x is even (x%2==0)<\/li>\n<li>The filter() function applies the lambda function to each element in mylist. It returns a filter object that contains only the elements where the lambda function returned True.<\/li>\n<li>The filter object is converted to a list and stored in mylist1. So mylist1 contains only the even numbers from mylist.<\/li>\n<li><strong>mystring contains a string:<\/strong> &#8220;Data Flair provide free course&#8221;.<\/li>\n<li>A lambda function is defined that checks if a character v is a vowel (a, e, i, o or u).<\/li>\n<li>The filter() function applies the lambda function to each character in mystring. It returns a filter object that<\/li>\n<li>contains only the vowels from the string.<\/li>\n<li>The filter object is converted to a list and stored in str1. So str1 contains only the vowels from mystring.<\/li>\n<li>Finally, both mylist1 and str1 are printed out.<\/li>\n<\/ul>\n<h3>Summary<\/h3>\n<p>In summary, this Python program offers a practical example of using the filter function with lambda expressions for selective element inclusion. It demonstrates how lambda expressions can be employed for concise and efficient conditions, making the code more readable and expressive. Understanding the filter function and lambda expressions enhances one&#8217;s ability to manipulate 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 with lambda expressions. Lambda expressions, also known as anonymous functions, provide a concise way to create small, one-line functions. The&#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":[28669,28668,28660,10333,28626,28667],"class_list":["post-123247","post","type-post","status-publish","format-standard","hentry","category-python","tag-filter-function","tag-filter-function-with-lambda-function-in-python","tag-lambda-function","tag-python","tag-python-practical","tag-python-program-on-filter-function-with-lambda-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 on Filter Function with Lambda Function - DataFlair<\/title>\n<meta name=\"description\" content=\"Python program offers a practical example of using the filter function with lambda expressions for selective element inclusion.\" \/>\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-on-filter-function-with-lambda-function\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Program on Filter Function with Lambda Function - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Python program offers a practical example of using the filter function with lambda expressions for selective element inclusion.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/python-program-on-filter-function-with-lambda-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:37:10+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-02-28T06:15:35+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 on Filter Function with Lambda Function - DataFlair","description":"Python program offers a practical example of using the filter function with lambda expressions for selective element inclusion.","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-on-filter-function-with-lambda-function\/","og_locale":"en_US","og_type":"article","og_title":"Python Program on Filter Function with Lambda Function - DataFlair","og_description":"Python program offers a practical example of using the filter function with lambda expressions for selective element inclusion.","og_url":"https:\/\/data-flair.training\/blogs\/python-program-on-filter-function-with-lambda-function\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2023-11-02T11:37:10+00:00","article_modified_time":"2024-02-28T06:15:35+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-on-filter-function-with-lambda-function\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/python-program-on-filter-function-with-lambda-function\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/c187795dc82ab948373cca526df7c445"},"headline":"Python Program on Filter Function with Lambda Function","datePublished":"2023-11-02T11:37:10+00:00","dateModified":"2024-02-28T06:15:35+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/python-program-on-filter-function-with-lambda-function\/"},"wordCount":420,"commentCount":1,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"keywords":["filter function","filter function with lambda function in python","lambda function","Python","python practical","python program on filter function with lambda function"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/python-program-on-filter-function-with-lambda-function\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/python-program-on-filter-function-with-lambda-function\/","url":"https:\/\/data-flair.training\/blogs\/python-program-on-filter-function-with-lambda-function\/","name":"Python Program on Filter Function with Lambda Function - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"datePublished":"2023-11-02T11:37:10+00:00","dateModified":"2024-02-28T06:15:35+00:00","description":"Python program offers a practical example of using the filter function with lambda expressions for selective element inclusion.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/python-program-on-filter-function-with-lambda-function\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/python-program-on-filter-function-with-lambda-function\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/python-program-on-filter-function-with-lambda-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 on Filter Function with Lambda 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\/123247","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=123247"}],"version-history":[{"count":2,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/123247\/revisions"}],"predecessor-version":[{"id":134134,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/123247\/revisions\/134134"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=123247"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=123247"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=123247"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}