{"id":183004,"date":"2024-03-13T10:24:00","date_gmt":"2024-03-13T09:24:00","guid":{"rendered":"https:\/\/liora.io\/en\/?p=183004"},"modified":"2026-02-06T08:24:48","modified_gmt":"2026-02-06T07:24:48","slug":"python-lambda-functions-principles-and-benefits","status":"publish","type":"post","link":"https:\/\/liora.io\/en\/python-lambda-functions-principles-and-benefits","title":{"rendered":"Python Lambda functions: principles and benefits"},"content":{"rendered":"\n.elementor-heading-title{padding:0;margin:0;line-height:1}.elementor-widget-heading .elementor-heading-title[class*=elementor-size-]&gt;a{color:inherit;font-size:inherit;line-height:inherit}.elementor-widget-heading .elementor-heading-title.elementor-size-small{font-size:15px}.elementor-widget-heading .elementor-heading-title.elementor-size-medium{font-size:19px}.elementor-widget-heading .elementor-heading-title.elementor-size-large{font-size:29px}.elementor-widget-heading .elementor-heading-title.elementor-size-xl{font-size:39px}.elementor-widget-heading .elementor-heading-title.elementor-size-xxl{font-size:59px}<p><strong>For small operations, regular functions in Python can take up a lot of space, making syntax difficult to read. This is why Lambda functions are so useful in Python. <\/strong><\/p>\t\t\n\t\t<p>How do you use them and why? Find out in this article.<\/p><p>&nbsp;<\/p><p>?Related articles:<\/p><table dir=\"ltr\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\" data-sheets-root=\"1\"><colgroup><col width=\"656\"><\/colgroup><tbody><tr><td data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;Matplotlib: Master Data Visualization in Python &quot;}\" data-sheets-hyperlink=\"https:\/\/liora.io\/en\/matplotlib-master-data-visualization-in-python\"><a href=\"https:\/\/liora.io\/en\/matplotlib-master-data-visualization-in-python\" target=\"_blank\" rel=\"noopener\">Matplotlib: Master Data Visualization in Python <\/a><\/td><\/tr><tr><td data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;Python Crash Course: Get started &quot;}\" data-sheets-hyperlink=\"https:\/\/liora.io\/en\/python-all-you-need-to-know\"><a href=\"https:\/\/liora.io\/en\/python-all-you-need-to-know\" target=\"_blank\" rel=\"noopener\">Python Crash Course: Get started <\/a><\/td><\/tr><tr><td data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;Mastering Machine Learning in Python: Data-Driven Success &quot;}\" data-sheets-hyperlink=\"https:\/\/liora.io\/en\/machine-learning-python-where-to-start\"><a href=\"https:\/\/liora.io\/en\/machine-learning-python-where-to-start\" target=\"_blank\" rel=\"noopener\">Mastering Machine Learning in Python: Data-Driven Success <\/a><\/td><\/tr><tr><td data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;Python Programming for Beginners - Episode 3&quot;}\" data-sheets-hyperlink=\"https:\/\/liora.io\/en\/python-programming-for-beginners-episode-3\"><a href=\"https:\/\/liora.io\/en\/python-programming-for-beginners-episode-3\" target=\"_blank\" rel=\"noopener\">Python Programming for Beginners &#8211; Episode 3<\/a><\/td><\/tr><tr><td data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;Django: All about the Python web development framework&quot;}\" data-sheets-hyperlink=\"https:\/\/liora.io\/en\/django-all-about-the-python-web-development-framework\"><a href=\"https:\/\/liora.io\/en\/django-all-about-the-python-web-development-framework\" target=\"_blank\" rel=\"noopener\">Django: All about the Python web development framework<\/a><\/td><\/tr><tr><td data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;NumPy : the most used Python library in Data Science&quot;}\" data-sheets-hyperlink=\"https:\/\/liora.io\/en\/numpy-the-python-library-in-data-science\"><a href=\"https:\/\/liora.io\/en\/numpy-the-python-library-in-data-science\" target=\"_blank\" rel=\"noopener\">NumPy : the most used Python library in Data Science<\/a><\/td><\/tr><\/tbody><\/table>\t\t\n\t\t\t<h3>Lambda functions in Python, anonymous functions<\/h3>\t\t\n\t\t<p>In Python, a Lambda function is an anonymous function. In other words, a function declared without a name. Its syntax is as follows:<\/p><pre style=\"padding-left: 40px\">Lambda arguments: expression<\/pre><p>As this is a<b> single-line syntax<\/b>, the function is much more readable. There&#8217;s no need to search for the argument on another line. Although the syntax is different from regular functions declared with the def keyword, they behave in the same way. It&#8217;s just <b>more concise. <\/b><\/p><p>Aside from its lack of a name and its conciseness, the Lambda functions in python are characterized by the fact that they contain <b>only one expression<\/b>. On the other hand, they can have several arguments. <\/p>\t\t\n\t\t\t<h3>4 exemples de fonctions Lambda<\/h3>\t\t\n\t\t<p>To help you better understand the difference between Python&#8217;s Lambda functions and regular functions, here are a few examples:<\/p>\t\t\n\t\t\t<h4>Lambda and sum functions<\/h4>\t\t\n\t\t<p>If you want to calculate the sum of two values, your code can be one of these two formulas.<\/p>\t\t\n\t\t\t\n.tg  {border-collapse:collapse;border-spacing:0;}\n.tg td{border-color:black;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px;\n  overflow:hidden;padding:10px 5px;word-break:normal;}\n.tg th{border-color:black;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px;\n  font-weight:normal;overflow:hidden;padding:10px 5px;word-break:normal;}\n.tg .tg-7vx0{font-family:Tahoma, Geneva, sans-serif !important;font-size:16px;font-weight:bold;text-align:center;vertical-align:top}\n.tg .tg-9kad{font-family:Tahoma, Geneva, sans-serif !important;font-size:16px;text-align:left;vertical-align:top}\n\n<table style=\"undefined;width: 510px\">\n<colgroup>\n<col style=\"width: 255px\">\n<col style=\"width: 255px\">\n<\/colgroup>\n<thead>\n  <tr>\n    <th>Regular Function in Python<\/th>\n    <th>Lambda Function in Python<\/th>\n  <\/tr>\n<\/thead>\n<tbody>\n  <tr>\n    <td>1 def sum_classic( a , b ):<br>2   return a + b<\/td>\n    <td>sum_Lambda = Lambda a,b : a+b<\/td>\n  <\/tr>\n<\/tbody>\n<\/table>\n\t\t\t\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex is-content-justification-center\"><div class=\"wp-block-button \"><a class=\"wp-block-button__link wp-element-button \" href=\"\/en\/courses\/data-ai\/\">Learn to use lambda functions<\/a><\/div><\/div>\n\n\t\t\t<h4>Lambda function and map  <\/h4>\t\t\n\t\t<p>With the map function, you can double each element of a list.<\/p><p>Here are the differences between the Lambda function and the regular function.<\/p>\t\t\n\t\t\t\n.tg  {border-collapse:collapse;border-spacing:0;}\n.tg td{border-color:black;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px;\n  overflow:hidden;padding:10px 5px;word-break:normal;}\n.tg th{border-color:black;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px;\n  font-weight:normal;overflow:hidden;padding:10px 5px;word-break:normal;}\n.tg .tg-7vx0{font-family:Tahoma, Geneva, sans-serif !important;font-size:16px;font-weight:bold;text-align:center;vertical-align:top}\n.tg .tg-n3o3{background-color:#efefef;font-family:Tahoma, Geneva, sans-serif !important;font-size:16px;text-align:left;\n  vertical-align:top}\n\n<table style=\"undefined;width: 510px\">\n<colgroup>\n<col style=\"width: 255px\">\n<col style=\"width: 255px\">\n<\/colgroup>\n<thead>\n  <tr>\n    <th>Regular Function in Python<\/th>\n    <th>Lambda Function in Python<\/th>\n  <\/tr>\n<\/thead>\n<tbody>\n  <tr>\n    <td>1 def doubler(x):<br>2   return x * 2<br>3 list = [1, 2, 3, 4, 5]<br>4 result_def = list(map(doubler, list))<br>5 print(result_def)  <\/td>\n    <td>1 list = [1, 2, 3, 4, 5]<br>2 result_lambda = list(map(lambda x: x * 2, list))<br>3 print(result_lambda)  <\/td>\n  <\/tr>\n<\/tbody>\n<\/table>\n\t\t<p>This will display [2, 4, 6, 8, 10].<\/p>\t\t\n\t\t\t<h4>Lambda and filter functions<\/h4>\t\t\n\t\t<p>With the map function, you can filter certain elements. In this example, the aim is to filter out even numbers from a list.<\/p><p>Here are the differences between the Lambda function and the regular function.<\/p>\t\t\n\t\t\t\n.tg  {border-collapse:collapse;border-spacing:0;}\n.tg td{border-color:black;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px;\n  overflow:hidden;padding:10px 5px;word-break:normal;}\n.tg th{border-color:black;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px;\n  font-weight:normal;overflow:hidden;padding:10px 5px;word-break:normal;}\n.tg .tg-7vx0{font-family:Tahoma, Geneva, sans-serif !important;font-size:16px;font-weight:bold;text-align:center;vertical-align:top}\n.tg .tg-n3o3{background-color:#efefef;font-family:Tahoma, Geneva, sans-serif !important;font-size:16px;text-align:left;\n  vertical-align:top}\n\n<table style=\"undefined;width: 510px\">\n<colgroup>\n<col style=\"width: 255px\">\n<col style=\"width: 255px\">\n<\/colgroup>\n<thead>\n  <tr>\n    <th>Regular Function in Python<\/th>\n    <th>Lambda Function in Python<\/th>\n  <\/tr>\n<\/thead>\n<tbody>\n  <tr>\n    <td>1 def is_even(x):<br>2 return x % 2 == 0<br>3 list = [1, 2, 3, 4, 5, 6, 7, 8, 9]<br>4 result_def = list(filter(is_even, list))<br>5 print(result_def)<\/td>\n    <td>1 list = [1, 2, 3, 4, 5, 6, 7, 8, 9]<br>2 result_lambda = list(filter(lambda x: x % 2 == 0, list))<br>3 print(result_lambda)  <\/td>\n  <\/tr>\n<\/tbody>\n<\/table>\n\t\t<p>In both cases, it will display [2, 4, 6, 8].<\/p>\t\t\n\t\t\t\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex is-content-justification-center\"><div class=\"wp-block-button \"><a class=\"wp-block-button__link wp-element-button \" href=\"\/en\/courses\/data-ai\/data-scientist\">Mastering lambda functions<\/a><\/div><\/div>\n\n\t\t\t<h4>Lambda and sorted functions<\/h4>\t\t\n\t\t<p>With the map function, you can sort a list. In this example, a list of tuples will be sorted according to the second element.<\/p><p>Here&#8217;s the code to type for both types of function:<\/p>\t\t\n\t\t\t\n.tg  {border-collapse:collapse;border-spacing:0;}\n.tg td{border-color:black;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px;\n  overflow:hidden;padding:10px 5px;word-break:normal;}\n.tg th{border-color:black;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px;\n  font-weight:normal;overflow:hidden;padding:10px 5px;word-break:normal;}\n.tg .tg-7vx0{font-family:Tahoma, Geneva, sans-serif !important;font-size:16px;font-weight:bold;text-align:center;vertical-align:top}\n.tg .tg-n3o3{background-color:#efefef;font-family:Tahoma, Geneva, sans-serif !important;font-size:16px;text-align:left;\n  vertical-align:top}\n\n<table style=\"undefined;width: 510px\">\n<colgroup>\n<col style=\"width: 255px\">\n<col style=\"width: 255px\">\n<\/colgroup>\n<thead>\n  <tr>\n    <th>Regular Function in Python<\/th>\n    <th>Lambda Function in Python<\/th>\n  <\/tr>\n<\/thead>\n<tbody>\n  <tr>\n    <td>1 def key_sort(tuple):<br>2   return tuple[1]<br>3 tuples = [(1, 5), (3, 2), (8, 10), (4, 7)]<br>4 result_def = sorted(tuples, key=key_sort)<br>5 print(result_def) <\/td>\n    <td>1 tuples = [(1, 5), (3, 2), (8, 10), (4, 7)] <br>2 result_lambda = sorted(tuples, key=lambda x: x[1])<br>3 print(result_lambda) <\/td>\n  <\/tr>\n<\/tbody>\n<\/table>\n\t\t<p>This will display [(3, 2), (1, 5), (4, 7), (8, 10)].<\/p><p>Apart from these examples, Lambda functions on Python are best used with simple functions for a one-off operation, such as apply(), sorted(), applymap() or reduce().<\/p>\t\t\n\t\t\t<h3>Lambda function use cases in Python<\/h3>\t\t\n\t\t<p>The Lambda function in Python can be used in the following situations:<\/p><ul><li><strong>Small operations:<\/strong> the return value is calculated by evaluating an expression on a single line of code. In this case, defining a complete function with def may seem too cumbersome.<\/li><li><strong>One-off operations:<\/strong> this means you don&#8217;t need to name the function, since it&#8217;s only used once. If you need to repeat it or refer to it elsewhere in the same module, it&#8217;s best to use normal functions.<\/li><li><strong>Built-in functions:<\/strong> such as the map(), filter(), apply(), sorted(), sum() and reduce() functions mentioned above.<\/li><\/ul><p>In practical terms,<strong> Lambda functions<\/strong> are useful for sorting Python data structures, such as lists and dictionaries. That said, the choice between Lambda and regular functions is mainly a question of style and readability, because in the end, the result is the same.<\/p><p><strong>Good to know:<\/strong> Lambda functions exist in many programming languages (Java, C#, C++). But with Python, they don&#8217;t add any extra functionality. This is not necessarily the case with other computer languages.<\/p>\t\t\n\t\t\t<h3>Use Lambda functions with Liora<\/h3>\t\t\n\t\t<p>Whether you&#8217;re using normal or Lambda functions with Python, you need to be well trained to master this programming language. With Liora, you can do just that. In our program, you&#8217;ll learn how to code all kinds of operations, from the simplest to the most complex.<\/p>\t\t\n\t\t\t\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex is-content-justification-center\"><div class=\"wp-block-button \"><a class=\"wp-block-button__link wp-element-button \" href=\"\/en\/courses\/data-ai\/data-scientist\">Register for our Data training course<\/a><\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>.elementor-heading-title{padding:0;margin:0;line-height:1}.elementor-widget-heading .elementor-heading-title[class*=elementor-size-]&gt;a{color:inherit;font-size:inherit;line-height:inherit}.elementor-widget-heading .elementor-heading-title.elementor-size-small{font-size:15px}.elementor-widget-heading .elementor-heading-title.elementor-size-medium{font-size:19px}.elementor-widget-heading .elementor-heading-title.elementor-size-large{font-size:29px}.elementor-widget-heading .elementor-heading-title.elementor-size-xl{font-size:39px}.elementor-widget-heading .elementor-heading-title.elementor-size-xxl{font-size:59px} For small operations, regular functions in Python can take up a lot of space, making syntax difficult to read. This is why Lambda functions are so useful in Python. How do you use them and why? Find out in this article. &nbsp; ?Related articles: Matplotlib: Master Data Visualization [&hellip;]<\/p>\n","protected":false},"author":76,"featured_media":183005,"comment_status":"open","ping_status":"open","sticky":false,"template":"elementor_theme","format":"standard","meta":{"_acf_changed":false,"editor_notices":[],"footnotes":""},"categories":[2434],"class_list":["post-183004","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cloud-dev"],"acf":[],"_links":{"self":[{"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/posts\/183004","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/users\/76"}],"replies":[{"embeddable":true,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/comments?post=183004"}],"version-history":[{"count":1,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/posts\/183004\/revisions"}],"predecessor-version":[{"id":205991,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/posts\/183004\/revisions\/205991"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/media\/183005"}],"wp:attachment":[{"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/media?parent=183004"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/categories?post=183004"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}