{"id":478,"date":"2020-02-02T17:23:37","date_gmt":"2020-02-03T01:23:37","guid":{"rendered":"https:\/\/renanmf.com\/?p=478"},"modified":"2021-04-30T16:34:55","modified_gmt":"2021-04-30T19:34:55","slug":"python-lambda-functions","status":"publish","type":"post","link":"https:\/\/renanmf.com\/python-lambda-functions\/","title":{"rendered":"Python Lambda Functions"},"content":{"rendered":"<p>A Python lambda function can only have one expression and no multiple lines.<\/p>\n<p>It is supposed to make it easier to create some small logic in one line instead of a whole function as it is usually done.<\/p>\n<p>Lambda functions are also anonymous, which means there is no need to name it.<\/p>\n<h2>Basic Syntax<\/h2>\n<p>The basic syntax is very simple, just use the <code><code>lambda<\/code><\/code> keyword, define the parameters needed, use &quot;:&quot; to separate the parameters from the expression.<\/p>\n<p>The general forms is:<\/p>\n<pre><code>lambda arguments : expression<\/code><\/pre>\n<h3>One parameter example<\/h3>\n<p>Look at this example using only one parameter<\/p>\n<pre><code class=\"language-python\">cubic = lambda number : number**3\nprint(cubic(2))\n#8<\/code><\/pre>\n<h3>Multiple parameter example<\/h3>\n<p>If you want, you can also have multiple parameters.<\/p>\n<pre><code class=\"language-python\">exponential = lambda multiplier, number, exponent : multiplier * number**exponent\nprint(exponential(2, 2, 3))\n#16<\/code><\/pre>\n<h3>Calling the Lambda Function directly<\/h3>\n<p>You don&#8217;t need to use a variable as we did before, you can make use of parenthesis around the lambda function and another pair of parenthesis around the arguments.<\/p>\n<p>The declaration of the function and the execution will happen in the same line.<\/p>\n<pre><code class=\"language-python\">(lambda multiplier, number, exponent : multiplier * number**exponent)(2, 2, 3)\n#16<\/code><\/pre>\n<h2>Examples using lambda functions with other built-in functions<\/h2>\n<h3>Map<\/h3>\n<p>The Map function applies the expression to each item in a list.<\/p>\n<p>Let&#8217;s calculate the cubic of each number in the list.<\/p>\n<pre><code class=\"language-python\">numbers = [2, 5, 10]\ncubics = list(map(lambda number : number**3, numbers))\nprint(cubics)\n#[8, 125, 1000]<\/code><\/pre>\n<h3>Filter<\/h3>\n<p>The Filter function will, obviously, filter the list based on the expression.<\/p>\n<p>Let&#8217;s filter to have only the numbers greater than 5.<\/p>\n<pre><code class=\"language-python\">numbers = [2, 5, 10]\nfiltered_list = list(filter(lambda number : number &gt; 5, numbers))\nprint(filtered_list)\n#[10]<\/code><\/pre>\n<h2>Watch on Youtube<\/h2>\n<p>You can also watch this content on Youtube:<\/p>\n<p><iframe loading=\"lazy\" width=\"560\" height=\"315\" src=\"https:\/\/www.youtube.com\/embed\/ddDp029Vxpk\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A Python lambda function can only have one expression and no multiple lines. It is supposed to make it easier to create some small logic in one line instead of a whole function as it is usually done. Lambda functions are also anonymous, which means there is no need to name it. Basic Syntax The [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":488,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[6],"class_list":["post-478","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-python"],"blocksy_meta":{"styles_descriptor":{"styles":{"desktop":"","tablet":"","mobile":""},"google_fonts":[],"version":6}},"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python Lambda Functions<\/title>\n<meta name=\"description\" content=\"Lambda functions are very handy when implementing some simple logic that fits in one line.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/renanmf.com\/python-lambda-functions\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Lambda Functions\" \/>\n<meta property=\"og:description\" content=\"Lambda functions are very handy when implementing some simple logic that fits in one line.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/renanmf.com\/python-lambda-functions\/\" \/>\n<meta property=\"og:site_name\" content=\"Renan Moura - Software Engineering\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/renanmouraf\" \/>\n<meta property=\"article:published_time\" content=\"2020-02-03T01:23:37+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-04-30T19:34:55+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/renanmf.com\/wp-content\/uploads\/2020\/02\/lambda-1.png\" \/>\n\t<meta property=\"og:image:width\" content=\"800\" \/>\n\t<meta property=\"og:image:height\" content=\"533\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Renan Moura\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/renanmouraf\" \/>\n<meta name=\"twitter:site\" content=\"@renanmouraf\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Renan Moura\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/renanmf.com\\\/python-lambda-functions\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/renanmf.com\\\/python-lambda-functions\\\/\"},\"author\":{\"name\":\"Renan Moura\",\"@id\":\"https:\\\/\\\/renanmf.com\\\/#\\\/schema\\\/person\\\/1a6fd46256318d200c1c8a867448e5a8\"},\"headline\":\"Python Lambda Functions\",\"datePublished\":\"2020-02-03T01:23:37+00:00\",\"dateModified\":\"2021-04-30T19:34:55+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/renanmf.com\\\/python-lambda-functions\\\/\"},\"wordCount\":223,\"publisher\":{\"@id\":\"https:\\\/\\\/renanmf.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/renanmf.com\\\/python-lambda-functions\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/renanmf.com\\\/wp-content\\\/uploads\\\/2020\\\/02\\\/lambda-1.png\",\"keywords\":[\"python\"],\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/renanmf.com\\\/python-lambda-functions\\\/\",\"url\":\"https:\\\/\\\/renanmf.com\\\/python-lambda-functions\\\/\",\"name\":\"Python Lambda Functions\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/renanmf.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/renanmf.com\\\/python-lambda-functions\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/renanmf.com\\\/python-lambda-functions\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/renanmf.com\\\/wp-content\\\/uploads\\\/2020\\\/02\\\/lambda-1.png\",\"datePublished\":\"2020-02-03T01:23:37+00:00\",\"dateModified\":\"2021-04-30T19:34:55+00:00\",\"description\":\"Lambda functions are very handy when implementing some simple logic that fits in one line.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/renanmf.com\\\/python-lambda-functions\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/renanmf.com\\\/python-lambda-functions\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/renanmf.com\\\/python-lambda-functions\\\/#primaryimage\",\"url\":\"https:\\\/\\\/renanmf.com\\\/wp-content\\\/uploads\\\/2020\\\/02\\\/lambda-1.png\",\"contentUrl\":\"https:\\\/\\\/renanmf.com\\\/wp-content\\\/uploads\\\/2020\\\/02\\\/lambda-1.png\",\"width\":800,\"height\":533,\"caption\":\"lambda\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/renanmf.com\\\/python-lambda-functions\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"In\u00edcio\",\"item\":\"https:\\\/\\\/renanmf.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python Lambda Functions\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/renanmf.com\\\/#website\",\"url\":\"https:\\\/\\\/renanmf.com\\\/\",\"name\":\"Renan Moura - Software Engineering\",\"description\":\"Software development, machine learning\",\"publisher\":{\"@id\":\"https:\\\/\\\/renanmf.com\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/renanmf.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/renanmf.com\\\/#organization\",\"name\":\"Renan Moura\",\"url\":\"https:\\\/\\\/renanmf.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/renanmf.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/renanmf.com\\\/wp-content\\\/uploads\\\/2020\\\/03\\\/me-e1583179172701.jpeg\",\"contentUrl\":\"https:\\\/\\\/renanmf.com\\\/wp-content\\\/uploads\\\/2020\\\/03\\\/me-e1583179172701.jpeg\",\"width\":120,\"height\":120,\"caption\":\"Renan Moura\"},\"image\":{\"@id\":\"https:\\\/\\\/renanmf.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/renanmouraf\",\"https:\\\/\\\/x.com\\\/renanmouraf\",\"https:\\\/\\\/instagram.com\\\/renanmouraf\",\"https:\\\/\\\/www.linkedin.com\\\/in\\\/renanmouraf\\\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/renanmf.com\\\/#\\\/schema\\\/person\\\/1a6fd46256318d200c1c8a867448e5a8\",\"name\":\"Renan Moura\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/efb78bdd04aa5627f80307aed5a9b31989d901c536d1e014a29a3c3591338af8?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/efb78bdd04aa5627f80307aed5a9b31989d901c536d1e014a29a3c3591338af8?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/efb78bdd04aa5627f80307aed5a9b31989d901c536d1e014a29a3c3591338af8?s=96&d=mm&r=g\",\"caption\":\"Renan Moura\"},\"description\":\"I'm a Software Engineer working in the industry for a decade now. I like to solve problems with as little code as possible. I\u2019m interested in solving all sorts of problems with technology in creative and innovative ways. From everyday shell scripts to machine learning models. I write about Software Development, Machine Learning, and Career in tech.\",\"sameAs\":[\"https:\\\/\\\/renanmf.com\\\/\",\"https:\\\/\\\/www.instagram.com\\\/renanmouraf\\\/\",\"https:\\\/\\\/www.linkedin.com\\\/in\\\/renanmouraf\\\/\",\"https:\\\/\\\/x.com\\\/https:\\\/\\\/twitter.com\\\/renanmouraf\"],\"url\":\"https:\\\/\\\/renanmf.com\\\/author\\\/renanmoura\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Lambda Functions","description":"Lambda functions are very handy when implementing some simple logic that fits in one line.","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:\/\/renanmf.com\/python-lambda-functions\/","og_locale":"en_US","og_type":"article","og_title":"Python Lambda Functions","og_description":"Lambda functions are very handy when implementing some simple logic that fits in one line.","og_url":"https:\/\/renanmf.com\/python-lambda-functions\/","og_site_name":"Renan Moura - Software Engineering","article_publisher":"https:\/\/www.facebook.com\/renanmouraf","article_published_time":"2020-02-03T01:23:37+00:00","article_modified_time":"2021-04-30T19:34:55+00:00","og_image":[{"width":800,"height":533,"url":"https:\/\/renanmf.com\/wp-content\/uploads\/2020\/02\/lambda-1.png","type":"image\/png"}],"author":"Renan Moura","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/renanmouraf","twitter_site":"@renanmouraf","twitter_misc":{"Written by":"Renan Moura","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/renanmf.com\/python-lambda-functions\/#article","isPartOf":{"@id":"https:\/\/renanmf.com\/python-lambda-functions\/"},"author":{"name":"Renan Moura","@id":"https:\/\/renanmf.com\/#\/schema\/person\/1a6fd46256318d200c1c8a867448e5a8"},"headline":"Python Lambda Functions","datePublished":"2020-02-03T01:23:37+00:00","dateModified":"2021-04-30T19:34:55+00:00","mainEntityOfPage":{"@id":"https:\/\/renanmf.com\/python-lambda-functions\/"},"wordCount":223,"publisher":{"@id":"https:\/\/renanmf.com\/#organization"},"image":{"@id":"https:\/\/renanmf.com\/python-lambda-functions\/#primaryimage"},"thumbnailUrl":"https:\/\/renanmf.com\/wp-content\/uploads\/2020\/02\/lambda-1.png","keywords":["python"],"articleSection":["Python"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/renanmf.com\/python-lambda-functions\/","url":"https:\/\/renanmf.com\/python-lambda-functions\/","name":"Python Lambda Functions","isPartOf":{"@id":"https:\/\/renanmf.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/renanmf.com\/python-lambda-functions\/#primaryimage"},"image":{"@id":"https:\/\/renanmf.com\/python-lambda-functions\/#primaryimage"},"thumbnailUrl":"https:\/\/renanmf.com\/wp-content\/uploads\/2020\/02\/lambda-1.png","datePublished":"2020-02-03T01:23:37+00:00","dateModified":"2021-04-30T19:34:55+00:00","description":"Lambda functions are very handy when implementing some simple logic that fits in one line.","breadcrumb":{"@id":"https:\/\/renanmf.com\/python-lambda-functions\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/renanmf.com\/python-lambda-functions\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/renanmf.com\/python-lambda-functions\/#primaryimage","url":"https:\/\/renanmf.com\/wp-content\/uploads\/2020\/02\/lambda-1.png","contentUrl":"https:\/\/renanmf.com\/wp-content\/uploads\/2020\/02\/lambda-1.png","width":800,"height":533,"caption":"lambda"},{"@type":"BreadcrumbList","@id":"https:\/\/renanmf.com\/python-lambda-functions\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"In\u00edcio","item":"https:\/\/renanmf.com\/"},{"@type":"ListItem","position":2,"name":"Python Lambda Functions"}]},{"@type":"WebSite","@id":"https:\/\/renanmf.com\/#website","url":"https:\/\/renanmf.com\/","name":"Renan Moura - Software Engineering","description":"Software development, machine learning","publisher":{"@id":"https:\/\/renanmf.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/renanmf.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/renanmf.com\/#organization","name":"Renan Moura","url":"https:\/\/renanmf.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/renanmf.com\/#\/schema\/logo\/image\/","url":"https:\/\/renanmf.com\/wp-content\/uploads\/2020\/03\/me-e1583179172701.jpeg","contentUrl":"https:\/\/renanmf.com\/wp-content\/uploads\/2020\/03\/me-e1583179172701.jpeg","width":120,"height":120,"caption":"Renan Moura"},"image":{"@id":"https:\/\/renanmf.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/renanmouraf","https:\/\/x.com\/renanmouraf","https:\/\/instagram.com\/renanmouraf","https:\/\/www.linkedin.com\/in\/renanmouraf\/"]},{"@type":"Person","@id":"https:\/\/renanmf.com\/#\/schema\/person\/1a6fd46256318d200c1c8a867448e5a8","name":"Renan Moura","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/efb78bdd04aa5627f80307aed5a9b31989d901c536d1e014a29a3c3591338af8?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/efb78bdd04aa5627f80307aed5a9b31989d901c536d1e014a29a3c3591338af8?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/efb78bdd04aa5627f80307aed5a9b31989d901c536d1e014a29a3c3591338af8?s=96&d=mm&r=g","caption":"Renan Moura"},"description":"I'm a Software Engineer working in the industry for a decade now. I like to solve problems with as little code as possible. I\u2019m interested in solving all sorts of problems with technology in creative and innovative ways. From everyday shell scripts to machine learning models. I write about Software Development, Machine Learning, and Career in tech.","sameAs":["https:\/\/renanmf.com\/","https:\/\/www.instagram.com\/renanmouraf\/","https:\/\/www.linkedin.com\/in\/renanmouraf\/","https:\/\/x.com\/https:\/\/twitter.com\/renanmouraf"],"url":"https:\/\/renanmf.com\/author\/renanmoura\/"}]}},"_links":{"self":[{"href":"https:\/\/renanmf.com\/wp-json\/wp\/v2\/posts\/478","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/renanmf.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/renanmf.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/renanmf.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/renanmf.com\/wp-json\/wp\/v2\/comments?post=478"}],"version-history":[{"count":7,"href":"https:\/\/renanmf.com\/wp-json\/wp\/v2\/posts\/478\/revisions"}],"predecessor-version":[{"id":3437,"href":"https:\/\/renanmf.com\/wp-json\/wp\/v2\/posts\/478\/revisions\/3437"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/renanmf.com\/wp-json\/wp\/v2\/media\/488"}],"wp:attachment":[{"href":"https:\/\/renanmf.com\/wp-json\/wp\/v2\/media?parent=478"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/renanmf.com\/wp-json\/wp\/v2\/categories?post=478"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/renanmf.com\/wp-json\/wp\/v2\/tags?post=478"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}