{"id":4516,"date":"2020-08-22T17:52:05","date_gmt":"2020-08-22T12:22:05","guid":{"rendered":"https:\/\/www.h2kinfosys.com\/blog\/?p=4516"},"modified":"2022-07-19T16:30:51","modified_gmt":"2022-07-19T11:00:51","slug":"python-lambda-functions-with-examples","status":"publish","type":"post","link":"https:\/\/www.h2kinfosys.com\/blog\/python-lambda-functions-with-examples\/","title":{"rendered":"Python Lambda Functions with Examples"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">What is the Lambda function?<\/h2>\n\n\n\n<p>While working with a complex system we need to divide the complex functions into small Lambda functions. For example, you are working on a complex calculator, one way is to put all the functions inside one function that is called when the user provides input and another way is to divide the function into many <a href=\"https:\/\/www.h2kinfosys.com\/blog\/python-functions-examples\/\">python functions<\/a>. We can create separate functions of sum, subtract, divide, and multiplication.&nbsp;<\/p>\n\n\n\n<p>Look at the function of the sum below.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def sum(x, y):\n\tresult = x + y\n\treturn result<\/code><\/pre>\n\n\n\n<p>The above code takes too much place. We can reduce it to one line code using lambda syntax.<\/p>\n\n\n\n<p>The given below is the sum lambda function<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sum = lambda a, b: a + b<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>def sum(x, y):\n\tresult = x + y\n\treturn result\nprint(\"normal function\")\nprint(sum(5, 5))\nprint(\"lambda function\")\nsum = lambda a, b: a + b\nprint(sum(5, 5))<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/7JlZybQIxRdf94QSxF4ouaT5sh15NqobJe0HNIwy9p9-7eobC_qE6cjeXUZETZ0m8HVlAX6fG4-GcX6hco9HYqoaTJChibLBfPyqbZxDZcx5NOiLiUy9Vn3AjOuWSNBJd-VdCQYmvi41aHNjvQ\" alt=\"What is Lambda function\" title=\"\"><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Why the lambda function?<\/h2>\n\n\n\n<p>Whenever you a function for a shorter time period you can use lambda functions. Lambda functions are usually practiced when you require to pass a function to bigger functions. Some functions take functions as arguments there lambda functions are very useful.\u00a0<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe title=\"Python Essential For Cloud Computing | Python Training For Beginners | H2kinfosys | Free Demo\" width=\"800\" height=\"450\" src=\"https:\/\/www.youtube.com\/embed\/FxMlefSzma0?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Example of function inside a function.<\/h2>\n\n\n\n<p>For example, we need a function that adds a number x and y. Y is in the programmer\u2019s hands and needs to be changed with time and X is given by the user.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def add(y):\n  return lambda x : x + y\nuser = add(2)\nprint(user(5))\nprint(user(2))<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/q6gjegp8_9xqunMMowYoma4emhf8OBK0uz6lUAyVaIUU87BvQPe60FsvKeqUF8wwtf7RGSMQrUW4e0ugzU26ax_lqqyTvWsVIzBRA-x6vzLsz26h6qt6ryd23d-acBu5wKEHATRpkjST92S2Cg\" alt=\"function inside a function\" title=\"\"><\/figure>\n\n\n\n<p>Now the programmer need the function to add 2 and 5 at the same time.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def add(y):\n return lambda x : x + y\nuser = add(2)\nuser1 = add(5)\nprint(user(5))\nprint(user1(5))<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh6.googleusercontent.com\/lY4uFysQzRyP-zeT_OVuGlWrazJKMGYJbtAnx7o4v0Z51WJdn5m3zleZL1nUCFsDHJ5EmImMgeI0g6C98uemOfwPLLyAmNJGWPb2Vs1URIFumwlT502QaSbtx-ZkmHpl_9pXZ72ZDGUYuQcH5A\" alt=\"\" title=\"\"><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Using the lambda function in the filter function<\/h2>\n\n\n\n<p>The filter function is used to filter the list or other <a href=\"https:\/\/en.wikipedia.org\/wiki\/Data_structure#:~:text=In%20computer%20science%2C%20a%20data,be%20applied%20to%20the%20data.\" rel=\"nofollow noopener\" target=\"_blank\">data structure<\/a>. The filter function takes a function and list as an argument.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>numbers = &#91;2, 3,7, 11, 14, 2, 6, 7, 17, 14, 0, 3, 21]\n\nfiltered_list = list(filter(lambda num: (num &gt; 10), numbers))\n\nprint(filtered_list)<\/code><\/pre>\n\n\n\n<p>Here the lambda function filters the list.<img fetchpriority=\"high\" decoding=\"async\" width=\"624\" height=\"196\" src=\"https:\/\/lh4.googleusercontent.com\/FvIhfBdaJibAiMQfQIpmqaB1q9RMAkv39uwT_kcdXHloqJWHMOeh6q_HMF1psvMMhxI8GHyuFqNPqtXxEMNYicPvkNRKxBOs730VOIfJOaq_EfyP7-UOt_ZvZmTxLxV0KM3epWC-GcXFi5G6Hw\" alt=\"\" title=\"\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>What is the Lambda function? While working with a complex system we need to divide the complex functions into small Lambda functions. For example, you are working on a complex calculator, one way is to put all the functions inside one function that is called when the user provides input and another way is to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":4540,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[342],"tags":[1269,1268,1267],"class_list":["post-4516","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-tutorials","tag-filter-function","tag-function-inside-a-function","tag-lambda-function"],"_links":{"self":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/4516","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/comments?post=4516"}],"version-history":[{"count":0,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/4516\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media\/4540"}],"wp:attachment":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media?parent=4516"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/categories?post=4516"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/tags?post=4516"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}