{"id":22290,"date":"2021-07-10T13:41:58","date_gmt":"2021-07-10T13:41:58","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=22290"},"modified":"2022-07-24T10:09:30","modified_gmt":"2022-07-24T10:09:30","slug":"python-anonymous-lambda-function-tutorials-with-example","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/python-anonymous-lambda-function-tutorials-with-example\/","title":{"rendered":"Python Tutorials: Anonymous\/Lambda Function Tutorials with Example"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\" id=\"use\">Use of Lambda Function&nbsp;in python<\/h2>\n\n\n\n<p>We use lambda functions when we require a nameless function for a short period of time.<\/p>\n\n\n\n<p>In <a href=\"http:\/\/www.datacamp.com\/learn\/python\" target=\"_blank\" rel=\"noreferrer noopener\">Python<\/a>, we generally use it as an argument to a higher-order function (a function that takes in other functions as&nbsp;<a href=\"https:\/\/www.programiz.com\/python-programming\/function-argument\" target=\"_blank\" rel=\"noopener\">arguments<\/a>). Lambda functions are used along with built-in functions like&nbsp;<code>filter()<\/code>,&nbsp;<code>map()<\/code>&nbsp;etc.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example use with filter()<\/h3>\n\n\n\n<p>The&nbsp;<code>filter()<\/code>&nbsp;function in Python takes in a function and a list as arguments.<\/p>\n\n\n\n<p>The function is called with all the items in the list and a new list is returned which contains items for which the function evaluates to&nbsp;<code>True<\/code>.<\/p>\n\n\n\n<p>Here is an example use of&nbsp;<code>filter()<\/code>&nbsp;function to filter out only even numbers from a list.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example use with map()<\/h3>\n\n\n\n<p>The&nbsp;<code>map()<\/code>&nbsp;function in Python takes in a function and a list.<\/p>\n\n\n\n<p>The function is called with all the items in the list and a new list is returned which contains items returned by that function for each item.<\/p>\n\n\n\n<p>Here is an example use of&nbsp;<code>map()<\/code>&nbsp;function to double all the items in a list.<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/devops-school\/112cf20522036f539102dffe030669ee.js\"><\/script>\n\n\n<div class=\"epyt-gallery\" data-currpage=\"1\" id=\"epyt_gallery_95465\"><iframe loading=\"lazy\"  id=\"_ytid_18680\"  width=\"760\" height=\"427\"  data-origwidth=\"760\" data-origheight=\"427\" src=\"https:\/\/www.youtube.com\/embed\/?enablejsapi=1&#038;autoplay=0&#038;cc_load_policy=0&#038;cc_lang_pref=&#038;iv_load_policy=1&#038;loop=0&#038;rel=1&#038;fs=1&#038;playsinline=0&#038;autohide=2&#038;theme=dark&#038;color=red&#038;controls=1&#038;disablekb=0&#038;\" class=\"__youtube_prefs__  no-lazyload\" title=\"YouTube player\"  data-epytgalleryid=\"epyt_gallery_95465\"  allow=\"fullscreen; accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen data-no-lazy=\"1\" data-skipgform_ajax_framebjll=\"\"><\/iframe><div class=\"epyt-gallery-list\"><div>Sorry, there was a YouTube error.<\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>Use of Lambda Function&nbsp;in python We use lambda functions when we require a nameless function for a short period of time. In Python, we generally use it as an argument&#8230; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[2],"tags":[],"class_list":["post-22290","post","type-post","status-publish","format-standard","hentry","category-uncategorised"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/22290","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=22290"}],"version-history":[{"count":4,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/22290\/revisions"}],"predecessor-version":[{"id":30718,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/22290\/revisions\/30718"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=22290"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=22290"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=22290"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}