{"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-08-09T19:43:13","modified_gmt":"2026-08-09T18:43:13","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<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>\n\n\n<p>How do you use them and why? Find out in this article.<\/p>\n\n\n<div class=\"wp-block-group has-background has-global-padding is-layout-constrained wp-container-core-group-is-layout-7441ce17 wp-block-group-is-layout-constrained\" style=\"border-left-color:#ff5c43;border-left-width:4px;border-radius:12px;background-color:#fff5f2;margin-top:32px;margin-bottom:32px;padding-top:24px;padding-right:28px;padding-bottom:24px;padding-left:28px\">\n\n<p style=\"margin-top:0;margin-bottom:16px;font-size:clamp(14.642px, 0.915rem + ((1vw - 3.2px) * 0.575), 22px);font-style:normal;font-weight:600\">Related articles<\/p>\n\n\n<ul class=\"wp-block-list\" style=\"margin-top:0;margin-bottom:0;padding-left:20px\">\n<li><a href=\"https:\/\/liora.io\/en\/matplotlib-master-data-visualization-in-python\" rel=\"noopener\" target=\"_blank\">Matplotlib: Master Data Visualization in Python <\/a><\/li>\n<li><a href=\"https:\/\/liora.io\/en\/python-all-you-need-to-know\" rel=\"noopener\" target=\"_blank\">Python Crash Course: Get started <\/a><\/li>\n<li><a href=\"https:\/\/liora.io\/en\/machine-learning-python-where-to-start\" rel=\"noopener\" target=\"_blank\">Mastering Machine Learning in Python: Data-Driven Success <\/a><\/li>\n<li><a href=\"https:\/\/liora.io\/en\/python-programming-for-beginners-episode-3\" rel=\"noopener\" target=\"_blank\">Python Programming for Beginners ,  Episode 3<\/a><\/li>\n<li><a href=\"https:\/\/liora.io\/en\/django-all-about-the-python-web-development-framework\" rel=\"noopener\" target=\"_blank\">Django: All about the Python web development framework<\/a><\/li>\n<li><a href=\"https:\/\/liora.io\/en\/numpy-the-python-library-in-data-science\" rel=\"noopener\" target=\"_blank\">NumPy : the most used Python library in Data Science<\/a><\/li>\n<\/ul>\n\n<\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"lambda-functions-in-python-anonymous-functions\">Lambda functions in Python, anonymous functions<\/h2>\n\n\n<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>\n\n\n<pre class=\"wp-block-code\"><code>Lambda arguments: expression<\/code><\/pre>\n\n\n<p>As this is a<strong> single-line syntax<\/strong>, 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 <strong>more concise. <\/strong><\/p>\n\n\n<p>Aside from its lack of a name and its conciseness, the Lambda functions in python are characterized by the fact that they contain <strong>only one expression<\/strong>. On the other hand, they can have several arguments. <\/p>\n\n\n<h2 class=\"wp-block-heading\" id=\"4-exemples-de-fonctions-lambda\">4 exemples de fonctions Lambda<\/h2>\n\n\n<p>To help you better understand the difference between Python&#8217;s Lambda functions and regular functions, here are a few examples:<\/p>\n\n\n<h3 class=\"wp-block-heading\" id=\"lambda-and-sum-functions\">Lambda and sum functions<\/h3>\n\n\n<p>If you want to calculate the sum of two values, your code can be one of these two formulas.<\/p>\n\n\n<p>.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}<\/p>\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table>\n<colgroup>\n<col\/>\n<col\/>\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><\/figure>\n\n\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex is-content-justification-center wp-container-core-buttons-is-layout-5ee10de4\" style=\"margin-top:32px;margin-bottom:32px\"><div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/liora.io\/en\/courses\/data-ai\/data-scientist\">Learn to use lambda functions<\/a><\/div><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"lambda-function-and-map\">Lambda function and map  <\/h3>\n\n\n<p>With the map function, you can double each element of a list.<\/p>\n\n\n<p>Here are the differences between the Lambda function and the regular function.<\/p>\n\n\n<p>.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}<\/p>\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table>\n<colgroup>\n<col\/>\n<col\/>\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><\/figure>\n\n\n<p>This will display [2, 4, 6, 8, 10].<\/p>\n\n\n<h3 class=\"wp-block-heading\" id=\"lambda-and-filter-functions\">Lambda and filter functions<\/h3>\n\n\n<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>\n\n\n<p>Here are the differences between the Lambda function and the regular function.<\/p>\n\n\n<p>.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}<\/p>\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table>\n<colgroup>\n<col\/>\n<col\/>\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><\/figure>\n\n\n<p>In both cases, it will display [2, 4, 6, 8].<\/p>\n\n\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex is-content-justification-center wp-container-core-buttons-is-layout-5ee10de4\" style=\"margin-top:32px;margin-bottom:32px\"><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\n<h3 class=\"wp-block-heading\" id=\"lambda-and-sorted-functions\">Lambda and sorted functions<\/h3>\n\n\n<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>\n\n\n<p>Here&#8217;s the code to type for both types of function:<\/p>\n\n\n<p>.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}<\/p>\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table>\n<colgroup>\n<col\/>\n<col\/>\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><\/figure>\n\n\n<p>This will display [(3, 2), (1, 5), (4, 7), (8, 10)].<\/p>\n\n\n<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>\n\n\n<h2 class=\"wp-block-heading\" id=\"lambda-function-use-cases-in-python\">Lambda function use cases in Python<\/h2>\n\n\n<p>The Lambda function in Python can be used in the following situations:<\/p>\n\n\n<ul class=\"wp-block-list\"><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>\n\n\n<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>\n\n\n<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>\n\n\n<h2 class=\"wp-block-heading\" id=\"use-lambda-functions-with-liora\">Use Lambda functions with Liora<\/h2>\n\n\n<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>\n\n\n","protected":false},"excerpt":{"rendered":"<p>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. Related articles Matplotlib: Master Data Visualization in Python Python Crash Course: Get started Mastering [&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":4,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/posts\/183004\/revisions"}],"predecessor-version":[{"id":211070,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/posts\/183004\/revisions\/211070"}],"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}]}}