{"id":79043,"date":"2020-06-10T13:11:17","date_gmt":"2020-06-10T07:41:17","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=79043"},"modified":"2020-06-10T13:11:17","modified_gmt":"2020-06-10T07:41:17","slug":"lambda-expression-in-python","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/lambda-expression-in-python\/","title":{"rendered":"Lambda Expression in Python"},"content":{"rendered":"<p>Python\u2019s Lambda expression are quite <strong>controversial<\/strong> in the <strong>programming circles<\/strong>. Some find it <strong>elegant<\/strong> and <strong>useful<\/strong>, while others freak out with the name of it.<\/p>\n<p>By the end of this article, you will be able to decide for yourselves whether you want them to be a part of your <strong>code <\/strong>or not. We\u2019ll help you get a clear understanding of what <strong>Lambda functions\/ expressions<\/strong> are. Also, you\u2019ll learn why and how you should <strong>use them<\/strong> in your <strong>program<\/strong>.<\/p>\n<p>Let\u2019s look at how <strong>Lambda expressions<\/strong> fit into your code!<\/p>\n<h3>What is Lambda function in Python?<\/h3>\n<p>Lambda functions are <strong>anonymous<\/strong> or <strong>nameless<\/strong>. Unlike <strong>traditional functions<\/strong>, <strong>lambda functions<\/strong> have <strong>no name<\/strong>. You can use <strong>anonymous functions<\/strong> when:<\/p>\n<p>a. You want to perform a <strong>simple operation<\/strong> and,<\/p>\n<p>b. You\u2019re going to <strong>use this<\/strong> <strong>function <\/strong>just <strong>once<\/strong>.<\/p>\n<p><strong>Syntax of Python Lambda expression<\/strong><\/p>\n<p><strong>lambda parameters: expression<\/strong><\/p>\n<p>The syntax of lambda expression contains three parts:<\/p>\n<ul>\n<li><strong>lambda<\/strong> &#8211; This is the keyword used to <strong>create<\/strong> a <strong>lambda function<\/strong><\/li>\n<li><strong>parameters<\/strong> &#8211; A lambda function can have <strong>zero<\/strong>, <strong>one<\/strong> or <strong>many parameters<\/strong>. You just need to specify<strong> comma-separated parameters<\/strong> after the lambda keyword.<\/li>\n<li><strong>expression<\/strong> &#8211; This is the <strong>body<\/strong> of the <strong>function<\/strong>. This is where you specify the <strong>operation performed<\/strong> by the <strong>function<\/strong>. A lambda function can have only <strong>one expression<\/strong>.<\/li>\n<\/ul>\n<p><strong>Python Lambda function Example<\/strong><\/p>\n<p><strong>a. Lambda function with one parameter:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; lambda x: x**2<\/pre>\n<p><strong>b. Lambda function with two parameters:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; lambda x, y: 2(x + y)<\/pre>\n<h3>How to call a Lambda Expression in Python?<\/h3>\n<p>A lambda function always <strong>returns<\/strong> a <strong>function object<\/strong>. So you can either <strong>call<\/strong> the <strong>lambda function directly<\/strong>:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; (lambda x: x ** 2)(10)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">100<br \/>\n&gt;&gt;&gt;<\/div>\n<p>or you can <strong>assign<\/strong> it a <strong>name<\/strong> and then you can <strong>call it<\/strong> like a <strong>traditional function call<\/strong>:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; square = lambda x: x ** 2\n&gt;&gt;&gt; square(10)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">100<br \/>\n&gt;&gt;&gt;<\/div>\n<h3>Lambda functions vs Traditional functions<\/h3>\n<p>Let\u2019s look at the difference with the help of an example.<\/p>\n<p><strong># traditional function to return the square of a number<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">def square(x):\nreturn x ** 2<\/pre>\n<p><strong># lambda function to calculate the square of a number<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">square_lambda = lambda x: x ** 2\n\nprint(square(10))\nprint(square_lambda(10))\n<\/pre>\n<p>All we\u2019ve done here is that we\u2019ve cleaned up some <strong>unnecessary syntax<\/strong> and <strong>changed some components<\/strong>. We use <strong>def<\/strong> to define a <strong>traditional function<\/strong>, whereas we use the <strong>lambda keyword<\/strong> to create a <strong>lambda<\/strong> <strong>function<\/strong>.<\/p>\n<p>Also, in an <strong>anonymous function<\/strong>, we omit the <strong>return<\/strong> <strong>keyword<\/strong>, thereby further reducing the <strong>number of lines<\/strong>.<\/p>\n<p>Other than these minor changes, let\u2019s compare the <strong>two types<\/strong> of functions at a greater depth.<\/p>\n<table>\n<tbody>\n<tr>\n<td><b>Traditional Functions<\/b><\/td>\n<td><b>Lambda Functions<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">They can have multiple expressions\/ statements in their body.<\/span><\/td>\n<td><span style=\"font-weight: 400\">They can have just a single expression in their body.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">These can have default values for their parameters.<\/span><\/td>\n<td><span style=\"font-weight: 400\">Lambda parameters can\u2019t have default values.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">Normal function can return an object of any type.<\/span><\/td>\n<td><span style=\"font-weight: 400\">Lambda function always returns a function object.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">It takes more time for execution as compared to lambda functions.<\/span><\/td>\n<td><span style=\"font-weight: 400\">These take relatively less time.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">Require more lines of code.<\/span><\/td>\n<td><span style=\"font-weight: 400\">It requires a maximum of two lines of code to define and call a lambda function.<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>How to use lambda Expression in your program?<\/h3>\n<p>One of the superpowers of lambda functions is that we can <strong>use<\/strong> them<strong> inside any normal function in Python<\/strong>. Thanks to the <strong>simplicity<\/strong> of lambda functions!<\/p>\n<p>This comes handy while working with <strong>high-order functions<\/strong>, i.e., the <strong>functions<\/strong> that take <strong>another function<\/strong> as an <strong>argument<\/strong>.<\/p>\n<p>Let\u2019s have a look at two such functions &#8211; <strong>filter() and map().<\/strong><\/p>\n<h3>Using lambda expression with filter()<\/h3>\n<p>The<strong> filter() function<\/strong> takes a <strong>function<\/strong> as its <strong>first argument<\/strong>. The <strong>second argument<\/strong> is an <strong>iterable<\/strong>, like, a <strong>list<\/strong>.<\/p>\n<p>The filter() function then applies the <strong>argument function<\/strong> to <strong>every element<\/strong> of the <strong>iterable<\/strong>. It then <strong>returns<\/strong> a <strong>new iterable<\/strong> that contains <strong>all the elements<\/strong> for which the <strong>function<\/strong> evaluates to <strong>True<\/strong>.<\/p>\n<p>Basically filter() <strong>filters out<\/strong> an <strong>iterable<\/strong> based on a <strong>function<\/strong>. It\u2019s like adding an <strong>if condition<\/strong> to the <strong>iterable<\/strong>.<\/p>\n<p>This is where we can make use of <strong>lambda functions<\/strong>.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; numbers = [10, 45, 23, 56, 6, 34, 78, 90, 3]\n&gt;&gt;&gt; filtered_list = list(filter(lambda x: x &gt; 50, numbers))\n&gt;&gt;&gt; print(filtered_list)<\/pre>\n<p><strong>Ouput<\/strong><\/p>\n<div class=\"code-output\">[56, 78, 90]<br \/>\n&gt;&gt;&gt;<\/div>\n<p>Here the<strong> filtered_list<\/strong> contains <strong>all elements<\/strong> from the <strong>numbers list<\/strong> which are <strong>greater<\/strong> <strong>than 50<\/strong>.<\/p>\n<h3>Using Python lambda expression with map()<\/h3>\n<p>Similar to <strong>filter()<\/strong>, the<strong> map()<\/strong> function takes a function as its <strong>first argument<\/strong>. And, the <strong>second argument<\/strong> is an<strong> iterable<\/strong>, like, a <strong>list<\/strong> or a <strong>tuple<\/strong>.<\/p>\n<p>The<strong> map()<\/strong> function <strong>calls<\/strong> the <strong>argument<\/strong> function on <strong>every element<\/strong> of the <strong>iterable<\/strong>. It then <strong>returns<\/strong> a <strong>new iterable<\/strong> that <strong>contains <\/strong>the <strong>elements<\/strong> <strong>returned<\/strong> by that <strong>function<\/strong> for <strong>each element<\/strong> of the <strong>original iterable<\/strong>.<\/p>\n<p>We can use <strong>lambda functions<\/strong> here as well.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; numbers = [10, 45, 23, 56, 6, 34, 78, 90, 3]\n&gt;&gt;&gt; new_numbers = list(map(lambda x: x + 2, numbers))\n&gt;&gt;&gt; print(new_numbers)<\/pre>\n<p><strong>Ouput<\/strong><\/p>\n<div class=\"code-output\">[12, 47, 25, 58, 8, 36, 80, 92, 5]<br \/>\n&gt;&gt;&gt;<\/div>\n<p>The list<strong> new_numbers<\/strong> contains the <strong>elements<\/strong> of <strong>numbers added<\/strong> by <strong>2<\/strong>.<\/p>\n<h3>Summary<\/h3>\n<p>This article comes to an end now. Hope you\u2019ve got a better <strong>understanding<\/strong> of <strong>lambda functions<\/strong> through our article.<\/p>\n<p>Lambda functions come in handy when you want to write quick <strong>\u201cuse-and-throw\u201d<\/strong> functions. If you find yourself writing a <strong>simple function<\/strong> which you\u2019re gonna use just once, think lambda! With Python, we have so many <strong>different features<\/strong> and <strong>concepts<\/strong> at our <strong>disposal<\/strong>.<\/p>\n<p>It\u2019s on us to understand them and use them to their <strong>full potential<\/strong>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python\u2019s Lambda expression are quite controversial in the programming circles. Some find it elegant and useful, while others freak out with the name of it. By the end of this article, you will be&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":79044,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1053],"tags":[2821,2822,2823,2824,2825],"class_list":["post-79043","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-lambda-function","tag-lambda-function-python","tag-lammbda-expression","tag-python-lambda-expression","tag-python-lambda-function"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Lambda Expression in Python - TechVidvan<\/title>\n<meta name=\"description\" content=\"What is Lambda Function in Python, its syntax and example, how to call python lambda expression, lambda vs traditional functions,filter() &amp; map() for lambda\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/lambda-expression-in-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Lambda Expression in Python - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"What is Lambda Function in Python, its syntax and example, how to call python lambda expression, lambda vs traditional functions,filter() &amp; map() for lambda\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/lambda-expression-in-python\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-06-10T07:41:17+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/06\/Lambda-Expression-in-Python.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"802\" \/>\n\t<meta property=\"og:image:height\" content=\"420\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Lambda Expression in Python - TechVidvan","description":"What is Lambda Function in Python, its syntax and example, how to call python lambda expression, lambda vs traditional functions,filter() & map() for lambda","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:\/\/techvidvan.com\/tutorials\/lambda-expression-in-python\/","og_locale":"en_US","og_type":"article","og_title":"Lambda Expression in Python - TechVidvan","og_description":"What is Lambda Function in Python, its syntax and example, how to call python lambda expression, lambda vs traditional functions,filter() & map() for lambda","og_url":"https:\/\/techvidvan.com\/tutorials\/lambda-expression-in-python\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2020-06-10T07:41:17+00:00","og_image":[{"width":802,"height":420,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/06\/Lambda-Expression-in-Python.jpg","type":"image\/jpeg"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/lambda-expression-in-python\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/lambda-expression-in-python\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"Lambda Expression in Python","datePublished":"2020-06-10T07:41:17+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/lambda-expression-in-python\/"},"wordCount":822,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/lambda-expression-in-python\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/06\/Lambda-Expression-in-Python.jpg","keywords":["Lambda function","lambda function python","lammbda expression","python lambda expression","python lambda function"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/lambda-expression-in-python\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/lambda-expression-in-python\/","url":"https:\/\/techvidvan.com\/tutorials\/lambda-expression-in-python\/","name":"Lambda Expression in Python - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/lambda-expression-in-python\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/lambda-expression-in-python\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/06\/Lambda-Expression-in-Python.jpg","datePublished":"2020-06-10T07:41:17+00:00","description":"What is Lambda Function in Python, its syntax and example, how to call python lambda expression, lambda vs traditional functions,filter() & map() for lambda","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/lambda-expression-in-python\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/lambda-expression-in-python\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/lambda-expression-in-python\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/06\/Lambda-Expression-in-Python.jpg","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/06\/Lambda-Expression-in-Python.jpg","width":802,"height":420,"caption":"Lambda expression in Python"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/lambda-expression-in-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Lambda Expression in Python"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/79043","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=79043"}],"version-history":[{"count":0,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/79043\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/79044"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=79043"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=79043"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=79043"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}