[{"data":1,"prerenderedAt":1450},["ShallowReactive",2],{"doc-\u002Fglossary\u002Fwhat-is-a-lambda-function-in-python":3},{"id":4,"title":5,"body":6,"description":1443,"extension":1444,"meta":1445,"navigation":402,"path":1446,"seo":1447,"stem":1448,"__hash__":1449},"content\u002Fglossary\u002Fwhat-is-a-lambda-function-in-python.md","What Is a Lambda Function in Python?",{"type":7,"value":8,"toc":1420},"minimark",[9,13,17,29,32,98,101,110,113,118,121,141,144,185,187,196,199,208,212,215,232,235,250,253,308,310,319,322,339,350,354,361,364,419,455,458,466,472,486,490,501,512,516,519,527,530,654,656,707,717,732,737,822,824,852,855,862,867,959,961,982,988,995,999,1002,1016,1019,1033,1039,1043,1046,1075,1077,1136,1139,1168,1171,1175,1178,1192,1195,1261,1264,1284,1288,1292,1295,1299,1302,1304,1351,1355,1358,1362,1368,1372,1416],[10,11,5],"h1",{"id":12},"what-is-a-lambda-function-in-python",[14,15,16],"p",{},"A lambda function in Python is a small function written in one line.",[14,18,19,20,24,25,28],{},"Beginners usually see lambda functions in examples with sorting, ",[21,22,23],"code",{},"map()",", or ",[21,26,27],{},"filter()",". They are useful for short tasks, but they are not a replacement for normal functions.",[14,30,31],{},"A lambda function works like a regular function. The main difference is that it is written in a shorter form.",[33,34,39],"pre",{"className":35,"code":36,"language":37,"meta":38,"style":38},"language-python shiki shiki-themes material-theme-lighter github-light github-dark","square = lambda x: x * x\nprint(square(5))\n","python","",[21,40,41,75],{"__ignoreMap":38},[42,43,46,50,54,58,62,66,69,72],"span",{"class":44,"line":45},"line",1,[42,47,49],{"class":48},"su5hD","square ",[42,51,53],{"class":52},"smGrS","=",[42,55,57],{"class":56},"sbsja"," lambda",[42,59,61],{"class":60},"sFwrP"," x",[42,63,65],{"class":64},"sP7_E",":",[42,67,68],{"class":48}," x ",[42,70,71],{"class":52},"*",[42,73,74],{"class":48}," x\n",[42,76,78,82,85,89,91,95],{"class":44,"line":77},2,[42,79,81],{"class":80},"sptTA","print",[42,83,84],{"class":64},"(",[42,86,88],{"class":87},"slqww","square",[42,90,84],{"class":64},[42,92,94],{"class":93},"srdBf","5",[42,96,97],{"class":64},"))\n",[14,99,100],{},"Output:",[33,102,104],{"className":35,"code":103,"language":37,"meta":38,"style":38},"25\n",[21,105,106],{"__ignoreMap":38},[42,107,108],{"class":44,"line":45},[42,109,103],{"class":93},[14,111,112],{},"A lambda function is a small anonymous function written in one line.",[114,115,117],"h2",{"id":116},"simple-definition","Simple definition",[14,119,120],{},"A lambda function:",[122,123,124,128,131,134],"ul",{},[125,126,127],"li",{},"is a short function written in one line",[125,129,130],{},"can take inputs and return a result",[125,132,133],{},"is often called an anonymous function because it does not need a normal function name",[125,135,136,137,140],{},"uses the ",[21,138,139],{},"lambda"," keyword",[14,142,143],{},"Here is a simple example:",[33,145,147],{"className":35,"code":146,"language":37,"meta":38,"style":38},"double = lambda x: x * 2\nprint(double(4))\n",[21,148,149,169],{"__ignoreMap":38},[42,150,151,154,156,158,160,162,164,166],{"class":44,"line":45},[42,152,153],{"class":48},"double ",[42,155,53],{"class":52},[42,157,57],{"class":56},[42,159,61],{"class":60},[42,161,65],{"class":64},[42,163,68],{"class":48},[42,165,71],{"class":52},[42,167,168],{"class":93}," 2\n",[42,170,171,173,175,178,180,183],{"class":44,"line":77},[42,172,81],{"class":80},[42,174,84],{"class":64},[42,176,177],{"class":87},"double",[42,179,84],{"class":64},[42,181,182],{"class":93},"4",[42,184,97],{"class":64},[14,186,100],{},[33,188,190],{"className":35,"code":189,"language":37,"meta":38,"style":38},"8\n",[21,191,192],{"__ignoreMap":38},[42,193,194],{"class":44,"line":45},[42,195,189],{"class":93},[14,197,198],{},"Even though lambda functions are often called anonymous, you can still store one in a variable, as shown above.",[14,200,201,202,207],{},"If you are new to functions, see ",[203,204,206],"a",{"href":205},"\u002Fglossary\u002Fwhat-is-a-function-in-python","what a function is in Python",".",[114,209,211],{"id":210},"basic-syntax","Basic syntax",[14,213,214],{},"The basic pattern is:",[33,216,218],{"className":35,"code":217,"language":37,"meta":38,"style":38},"lambda parameters: expression\n",[21,219,220],{"__ignoreMap":38},[42,221,222,224,227,229],{"class":44,"line":45},[42,223,139],{"class":56},[42,225,226],{"class":60}," parameters",[42,228,65],{"class":64},[42,230,231],{"class":48}," expression\n",[14,233,234],{},"How to read this:",[122,236,237,240,243],{},[125,238,239],{},"the part before the colon is the input",[125,241,242],{},"the part after the colon is the value that gets returned",[125,244,245,246,249],{},"there is no ",[21,247,248],{},"return"," keyword inside a lambda",[14,251,252],{},"Example:",[33,254,256],{"className":35,"code":255,"language":37,"meta":38,"style":38},"add = lambda a, b: a + b\nprint(add(3, 7))\n",[21,257,258,287],{"__ignoreMap":38},[42,259,260,263,265,267,270,273,276,278,281,284],{"class":44,"line":45},[42,261,262],{"class":48},"add ",[42,264,53],{"class":52},[42,266,57],{"class":56},[42,268,269],{"class":60}," a",[42,271,272],{"class":64},",",[42,274,275],{"class":60}," b",[42,277,65],{"class":64},[42,279,280],{"class":48}," a ",[42,282,283],{"class":52},"+",[42,285,286],{"class":48}," b\n",[42,288,289,291,293,296,298,301,303,306],{"class":44,"line":77},[42,290,81],{"class":80},[42,292,84],{"class":64},[42,294,295],{"class":87},"add",[42,297,84],{"class":64},[42,299,300],{"class":93},"3",[42,302,272],{"class":64},[42,304,305],{"class":93}," 7",[42,307,97],{"class":64},[14,309,100],{},[33,311,313],{"className":35,"code":312,"language":37,"meta":38,"style":38},"10\n",[21,314,315],{"__ignoreMap":38},[42,316,317],{"class":44,"line":45},[42,318,312],{"class":93},[14,320,321],{},"In this example:",[122,323,324,333],{},[125,325,326,328,329,332],{},[21,327,203],{}," and ",[21,330,331],{},"b"," are parameters",[125,334,335,338],{},[21,336,337],{},"a + b"," is the expression that Python returns",[14,340,341,342,328,346,207],{},"If you want to understand the difference between inputs in a function definition and values passed into a function call, see ",[203,343,345],{"href":344},"\u002Fglossary\u002Fwhat-is-a-parameter-in-python","what a parameter is in Python",[203,347,349],{"href":348},"\u002Fglossary\u002Fwhat-is-an-argument-in-python","what an argument is in Python",[114,351,353],{"id":352},"how-it-compares-to-def","How it compares to def",[14,355,356,357,360],{},"A regular function uses ",[21,358,359],{},"def"," and can contain multiple lines. A lambda function is limited to a single expression.",[14,362,363],{},"These two examples do the same thing:",[33,365,367],{"className":35,"code":366,"language":37,"meta":38,"style":38},"def square(x):\n    return x * x\n\nprint(square(5))\n",[21,368,369,385,397,404],{"__ignoreMap":38},[42,370,371,373,377,379,382],{"class":44,"line":45},[42,372,359],{"class":56},[42,374,376],{"class":375},"sGLFI"," square",[42,378,84],{"class":64},[42,380,381],{"class":60},"x",[42,383,384],{"class":64},"):\n",[42,386,387,391,393,395],{"class":44,"line":77},[42,388,390],{"class":389},"sVHd0","    return",[42,392,68],{"class":48},[42,394,71],{"class":52},[42,396,74],{"class":48},[42,398,400],{"class":44,"line":399},3,[42,401,403],{"emptyLinePlaceholder":402},true,"\n",[42,405,407,409,411,413,415,417],{"class":44,"line":406},4,[42,408,81],{"class":80},[42,410,84],{"class":64},[42,412,88],{"class":87},[42,414,84],{"class":64},[42,416,94],{"class":93},[42,418,97],{"class":64},[33,420,421],{"className":35,"code":36,"language":37,"meta":38,"style":38},[21,422,423,441],{"__ignoreMap":38},[42,424,425,427,429,431,433,435,437,439],{"class":44,"line":45},[42,426,49],{"class":48},[42,428,53],{"class":52},[42,430,57],{"class":56},[42,432,61],{"class":60},[42,434,65],{"class":64},[42,436,68],{"class":48},[42,438,71],{"class":52},[42,440,74],{"class":48},[42,442,443,445,447,449,451,453],{"class":44,"line":77},[42,444,81],{"class":80},[42,446,84],{"class":64},[42,448,88],{"class":87},[42,450,84],{"class":64},[42,452,94],{"class":93},[42,454,97],{"class":64},[14,456,457],{},"Output for both:",[33,459,460],{"className":35,"code":103,"language":37,"meta":38,"style":38},[21,461,462],{"__ignoreMap":38},[42,463,464],{"class":44,"line":45},[42,465,103],{"class":93},[14,467,468,469,471],{},"Use ",[21,470,359],{}," when:",[122,473,474,477,480,483],{},[125,475,476],{},"the logic is longer",[125,478,479],{},"you need multiple steps",[125,481,482],{},"you want a clear function name",[125,484,485],{},"readability matters more than saving space",[14,487,468,488,471],{},[21,489,139],{},[122,491,492,495,498],{},[125,493,494],{},"the operation is short",[125,496,497],{},"you only need the function once",[125,499,500],{},"the code stays easy to read",[14,502,503,504,328,508,207],{},"For a broader explanation, see ",[203,505,507],{"href":506},"\u002Flearn\u002Fpython-functions-explained","Python functions explained",[203,509,511],{"href":510},"\u002Flearn\u002Flambda-functions-in-python-explained","lambda functions in Python explained",[114,513,515],{"id":514},"where-beginners-may-see-lambda","Where beginners may see lambda",[14,517,518],{},"Beginners often see lambda used when a function is passed into another function.",[520,521,523,524],"h3",{"id":522},"with-sorted","With ",[21,525,526],{},"sorted()",[14,528,529],{},"A common example is sorting data by a specific part of each item.",[33,531,533],{"className":35,"code":532,"language":37,"meta":38,"style":38},"students = [(\"Maya\", 82), (\"Leo\", 91), (\"Ava\", 78)]\n\nresult = sorted(students, key=lambda student: student[1])\nprint(result)\n",[21,534,535,597,601,642],{"__ignoreMap":38},[42,536,537,540,542,545,549,553,555,557,560,563,566,568,571,573,575,578,580,582,584,587,589,591,594],{"class":44,"line":45},[42,538,539],{"class":48},"students ",[42,541,53],{"class":52},[42,543,544],{"class":64}," [(",[42,546,548],{"class":547},"sjJ54","\"",[42,550,552],{"class":551},"s_sjI","Maya",[42,554,548],{"class":547},[42,556,272],{"class":64},[42,558,559],{"class":93}," 82",[42,561,562],{"class":64},"),",[42,564,565],{"class":64}," (",[42,567,548],{"class":547},[42,569,570],{"class":551},"Leo",[42,572,548],{"class":547},[42,574,272],{"class":64},[42,576,577],{"class":93}," 91",[42,579,562],{"class":64},[42,581,565],{"class":64},[42,583,548],{"class":547},[42,585,586],{"class":551},"Ava",[42,588,548],{"class":547},[42,590,272],{"class":64},[42,592,593],{"class":93}," 78",[42,595,596],{"class":64},")]\n",[42,598,599],{"class":44,"line":77},[42,600,403],{"emptyLinePlaceholder":402},[42,602,603,606,608,611,613,616,618,622,624,626,629,631,633,636,639],{"class":44,"line":399},[42,604,605],{"class":48},"result ",[42,607,53],{"class":52},[42,609,610],{"class":80}," sorted",[42,612,84],{"class":64},[42,614,615],{"class":87},"students",[42,617,272],{"class":64},[42,619,621],{"class":620},"s99_P"," key",[42,623,53],{"class":52},[42,625,139],{"class":56},[42,627,628],{"class":60}," student",[42,630,65],{"class":64},[42,632,628],{"class":87},[42,634,635],{"class":64},"[",[42,637,638],{"class":93},"1",[42,640,641],{"class":64},"])\n",[42,643,644,646,648,651],{"class":44,"line":406},[42,645,81],{"class":80},[42,647,84],{"class":64},[42,649,650],{"class":87},"result",[42,652,653],{"class":64},")\n",[14,655,100],{},[33,657,659],{"className":35,"code":658,"language":37,"meta":38,"style":38},"[('Ava', 78), ('Maya', 82), ('Leo', 91)]\n",[21,660,661],{"__ignoreMap":38},[42,662,663,666,669,671,673,675,677,679,681,683,685,687,689,691,693,695,697,699,701,703,705],{"class":44,"line":45},[42,664,665],{"class":64},"[(",[42,667,668],{"class":547},"'",[42,670,586],{"class":551},[42,672,668],{"class":547},[42,674,272],{"class":64},[42,676,593],{"class":93},[42,678,562],{"class":64},[42,680,565],{"class":64},[42,682,668],{"class":547},[42,684,552],{"class":551},[42,686,668],{"class":547},[42,688,272],{"class":64},[42,690,559],{"class":93},[42,692,562],{"class":64},[42,694,565],{"class":64},[42,696,668],{"class":547},[42,698,570],{"class":551},[42,700,668],{"class":547},[42,702,272],{"class":64},[42,704,577],{"class":93},[42,706,596],{"class":64},[14,708,709,710,713,714,716],{},"Here, ",[21,711,712],{},"lambda student: student[1]"," tells ",[21,715,526],{}," to use the second value in each tuple for sorting.",[14,718,719,720,727,728,731],{},"See ",[203,721,723,724,726],{"href":722},"\u002Freference\u002Fpython-sorted-function-explained","Python ",[21,725,526],{}," function explained"," if you want to learn more about the ",[21,729,730],{},"key"," argument.",[520,733,523,735],{"id":734},"with-map",[21,736,23],{},[33,738,740],{"className":35,"code":739,"language":37,"meta":38,"style":38},"numbers = [1, 2, 3, 4]\n\nresult = list(map(lambda x: x * 2, numbers))\nprint(result)\n",[21,741,742,772,776,812],{"__ignoreMap":38},[42,743,744,747,749,752,754,756,759,761,764,766,769],{"class":44,"line":45},[42,745,746],{"class":48},"numbers ",[42,748,53],{"class":52},[42,750,751],{"class":64}," [",[42,753,638],{"class":93},[42,755,272],{"class":64},[42,757,758],{"class":93}," 2",[42,760,272],{"class":64},[42,762,763],{"class":93}," 3",[42,765,272],{"class":64},[42,767,768],{"class":93}," 4",[42,770,771],{"class":64},"]\n",[42,773,774],{"class":44,"line":77},[42,775,403],{"emptyLinePlaceholder":402},[42,777,778,780,782,786,788,791,793,795,797,799,801,803,805,807,810],{"class":44,"line":399},[42,779,605],{"class":48},[42,781,53],{"class":52},[42,783,785],{"class":784},"sZMiF"," list",[42,787,84],{"class":64},[42,789,790],{"class":80},"map",[42,792,84],{"class":64},[42,794,139],{"class":56},[42,796,61],{"class":60},[42,798,65],{"class":64},[42,800,68],{"class":87},[42,802,71],{"class":52},[42,804,758],{"class":93},[42,806,272],{"class":64},[42,808,809],{"class":87}," numbers",[42,811,97],{"class":64},[42,813,814,816,818,820],{"class":44,"line":406},[42,815,81],{"class":80},[42,817,84],{"class":64},[42,819,650],{"class":87},[42,821,653],{"class":64},[14,823,100],{},[33,825,827],{"className":35,"code":826,"language":37,"meta":38,"style":38},"[2, 4, 6, 8]\n",[21,828,829],{"__ignoreMap":38},[42,830,831,833,836,838,840,842,845,847,850],{"class":44,"line":45},[42,832,635],{"class":64},[42,834,835],{"class":93},"2",[42,837,272],{"class":64},[42,839,768],{"class":93},[42,841,272],{"class":64},[42,843,844],{"class":93}," 6",[42,846,272],{"class":64},[42,848,849],{"class":93}," 8",[42,851,771],{"class":64},[14,853,854],{},"This applies the lambda function to each item in the list.",[14,856,719,857,207],{},[203,858,723,860,726],{"href":859},"\u002Freference\u002Fpython-map-function-explained",[21,861,23],{},[520,863,523,865],{"id":864},"with-filter",[21,866,27],{},[33,868,870],{"className":35,"code":869,"language":37,"meta":38,"style":38},"numbers = [1, 2, 3, 4, 5, 6]\n\nresult = list(filter(lambda x: x % 2 == 0, numbers))\nprint(result)\n",[21,871,872,905,909,949],{"__ignoreMap":38},[42,873,874,876,878,880,882,884,886,888,890,892,894,896,899,901,903],{"class":44,"line":45},[42,875,746],{"class":48},[42,877,53],{"class":52},[42,879,751],{"class":64},[42,881,638],{"class":93},[42,883,272],{"class":64},[42,885,758],{"class":93},[42,887,272],{"class":64},[42,889,763],{"class":93},[42,891,272],{"class":64},[42,893,768],{"class":93},[42,895,272],{"class":64},[42,897,898],{"class":93}," 5",[42,900,272],{"class":64},[42,902,844],{"class":93},[42,904,771],{"class":64},[42,906,907],{"class":44,"line":77},[42,908,403],{"emptyLinePlaceholder":402},[42,910,911,913,915,917,919,922,924,926,928,930,932,935,937,940,943,945,947],{"class":44,"line":399},[42,912,605],{"class":48},[42,914,53],{"class":52},[42,916,785],{"class":784},[42,918,84],{"class":64},[42,920,921],{"class":80},"filter",[42,923,84],{"class":64},[42,925,139],{"class":56},[42,927,61],{"class":60},[42,929,65],{"class":64},[42,931,68],{"class":87},[42,933,934],{"class":52},"%",[42,936,758],{"class":93},[42,938,939],{"class":52}," ==",[42,941,942],{"class":93}," 0",[42,944,272],{"class":64},[42,946,809],{"class":87},[42,948,97],{"class":64},[42,950,951,953,955,957],{"class":44,"line":406},[42,952,81],{"class":80},[42,954,84],{"class":64},[42,956,650],{"class":87},[42,958,653],{"class":64},[14,960,100],{},[33,962,964],{"className":35,"code":963,"language":37,"meta":38,"style":38},"[2, 4, 6]\n",[21,965,966],{"__ignoreMap":38},[42,967,968,970,972,974,976,978,980],{"class":44,"line":45},[42,969,635],{"class":64},[42,971,835],{"class":93},[42,973,272],{"class":64},[42,975,768],{"class":93},[42,977,272],{"class":64},[42,979,844],{"class":93},[42,981,771],{"class":64},[14,983,984,985,207],{},"This keeps only the values where the lambda returns ",[21,986,987],{},"True",[14,989,719,990,207],{},[203,991,723,993,726],{"href":992},"\u002Freference\u002Fpython-filter-function-explained",[21,994,27],{},[114,996,998],{"id":997},"when-to-use-it","When to use it",[14,1000,1001],{},"Use a lambda function when:",[122,1003,1004,1007,1010],{},[125,1005,1006],{},"you need a small one-time function",[125,1008,1009],{},"the code is still easy to read",[125,1011,1012,1013,1015],{},"writing a full ",[21,1014,359],{}," function would feel unnecessary",[14,1017,1018],{},"Avoid lambda when:",[122,1020,1021,1024,1027,1030],{},[125,1022,1023],{},"the expression becomes hard to understand",[125,1025,1026],{},"you need multiple statements",[125,1028,1029],{},"the function will be reused often",[125,1031,1032],{},"a named function would make the code clearer",[14,1034,1035,1036,1038],{},"A good rule for beginners is simple: if the lambda makes the code harder to read, use ",[21,1037,359],{}," instead.",[114,1040,1042],{"id":1041},"common-beginner-confusion","Common beginner confusion",[14,1044,1045],{},"Some common points are easy to misunderstand:",[122,1047,1048,1055,1061,1067],{},[125,1049,1050,1054],{},[1051,1052,1053],"strong",{},"Lambda is still a function."," It is not a special separate kind of thing.",[125,1056,1057,1060],{},[1051,1058,1059],{},"It can be stored in a variable."," You can assign it just like other values.",[125,1062,1063,1066],{},[1051,1064,1065],{},"It does not run automatically."," Creating a lambda only creates the function. You still need to call it.",[125,1068,1069,1074],{},[1051,1070,1071,1072,207],{},"It is not always better than ",[21,1073,359],{}," Shorter code is not always clearer code.",[14,1076,252],{},[33,1078,1080],{"className":35,"code":1079,"language":37,"meta":38,"style":38},"my_lambda = lambda x: x + 1\n\nprint(type(my_lambda))\nprint(my_lambda(3))\n",[21,1081,1082,1102,1106,1122],{"__ignoreMap":38},[42,1083,1084,1087,1089,1091,1093,1095,1097,1099],{"class":44,"line":45},[42,1085,1086],{"class":48},"my_lambda ",[42,1088,53],{"class":52},[42,1090,57],{"class":56},[42,1092,61],{"class":60},[42,1094,65],{"class":64},[42,1096,68],{"class":48},[42,1098,283],{"class":52},[42,1100,1101],{"class":93}," 1\n",[42,1103,1104],{"class":44,"line":77},[42,1105,403],{"emptyLinePlaceholder":402},[42,1107,1108,1110,1112,1115,1117,1120],{"class":44,"line":399},[42,1109,81],{"class":80},[42,1111,84],{"class":64},[42,1113,1114],{"class":784},"type",[42,1116,84],{"class":64},[42,1118,1119],{"class":87},"my_lambda",[42,1121,97],{"class":64},[42,1123,1124,1126,1128,1130,1132,1134],{"class":44,"line":406},[42,1125,81],{"class":80},[42,1127,84],{"class":64},[42,1129,1119],{"class":87},[42,1131,84],{"class":64},[42,1133,300],{"class":93},[42,1135,97],{"class":64},[14,1137,1138],{},"Possible output:",[33,1140,1142],{"className":35,"code":1141,"language":37,"meta":38,"style":38},"\u003Cclass 'function'>\n4\n",[21,1143,1144,1163],{"__ignoreMap":38},[42,1145,1146,1149,1152,1155,1158,1160],{"class":44,"line":45},[42,1147,1148],{"class":52},"\u003C",[42,1150,1151],{"class":56},"class",[42,1153,1154],{"class":547}," '",[42,1156,1157],{"class":551},"function",[42,1159,668],{"class":547},[42,1161,1162],{"class":52},">\n",[42,1164,1165],{"class":44,"line":77},[42,1166,1167],{"class":93},"4\n",[14,1169,1170],{},"This shows that a lambda is a normal function object.",[114,1172,1174],{"id":1173},"common-mistakes","Common mistakes",[14,1176,1177],{},"Beginners often run into these problems:",[122,1179,1180,1183,1186,1189],{},[125,1181,1182],{},"Thinking lambda is a different kind of object instead of a function",[125,1184,1185],{},"Trying to write multiple statements inside a lambda",[125,1187,1188],{},"Using lambda where a normal named function would be easier to read",[125,1190,1191],{},"Confusing parameters with arguments in lambda examples",[14,1193,1194],{},"For quick checking, these commands can help:",[33,1196,1198],{"className":35,"code":1197,"language":37,"meta":38,"style":38},"print(type(my_lambda))\nprint(my_lambda(3))\nhelp(sorted)\nhelp(map)\nhelp(filter)\n",[21,1199,1200,1214,1228,1240,1250],{"__ignoreMap":38},[42,1201,1202,1204,1206,1208,1210,1212],{"class":44,"line":45},[42,1203,81],{"class":80},[42,1205,84],{"class":64},[42,1207,1114],{"class":784},[42,1209,84],{"class":64},[42,1211,1119],{"class":87},[42,1213,97],{"class":64},[42,1215,1216,1218,1220,1222,1224,1226],{"class":44,"line":77},[42,1217,81],{"class":80},[42,1219,84],{"class":64},[42,1221,1119],{"class":87},[42,1223,84],{"class":64},[42,1225,300],{"class":93},[42,1227,97],{"class":64},[42,1229,1230,1233,1235,1238],{"class":44,"line":399},[42,1231,1232],{"class":80},"help",[42,1234,84],{"class":64},[42,1236,1237],{"class":80},"sorted",[42,1239,653],{"class":64},[42,1241,1242,1244,1246,1248],{"class":44,"line":406},[42,1243,1232],{"class":80},[42,1245,84],{"class":64},[42,1247,790],{"class":80},[42,1249,653],{"class":64},[42,1251,1253,1255,1257,1259],{"class":44,"line":1252},5,[42,1254,1232],{"class":80},[42,1256,84],{"class":64},[42,1258,921],{"class":80},[42,1260,653],{"class":64},[14,1262,1263],{},"These can help you confirm:",[122,1265,1266,1269,1272],{},[125,1267,1268],{},"whether your lambda is really a function",[125,1270,1271],{},"what result it returns",[125,1273,1274,1275,1277,1278,1280,1281,1283],{},"how functions like ",[21,1276,526],{},", ",[21,1279,23],{},", and ",[21,1282,27],{}," expect lambda to be used",[114,1285,1287],{"id":1286},"faq","FAQ",[520,1289,1291],{"id":1290},"does-a-lambda-function-return-a-value","Does a lambda function return a value?",[14,1293,1294],{},"Yes. The expression after the colon is returned automatically.",[520,1296,1298],{"id":1297},"can-a-lambda-function-have-more-than-one-parameter","Can a lambda function have more than one parameter?",[14,1300,1301],{},"Yes. You can write multiple parameters separated by commas.",[14,1303,252],{},[33,1305,1307],{"className":35,"code":1306,"language":37,"meta":38,"style":38},"multiply = lambda a, b: a * b\nprint(multiply(2, 5))\n",[21,1308,1309,1332],{"__ignoreMap":38},[42,1310,1311,1314,1316,1318,1320,1322,1324,1326,1328,1330],{"class":44,"line":45},[42,1312,1313],{"class":48},"multiply ",[42,1315,53],{"class":52},[42,1317,57],{"class":56},[42,1319,269],{"class":60},[42,1321,272],{"class":64},[42,1323,275],{"class":60},[42,1325,65],{"class":64},[42,1327,280],{"class":48},[42,1329,71],{"class":52},[42,1331,286],{"class":48},[42,1333,1334,1336,1338,1341,1343,1345,1347,1349],{"class":44,"line":77},[42,1335,81],{"class":80},[42,1337,84],{"class":64},[42,1339,1340],{"class":87},"multiply",[42,1342,84],{"class":64},[42,1344,835],{"class":93},[42,1346,272],{"class":64},[42,1348,898],{"class":93},[42,1350,97],{"class":64},[520,1352,1354],{"id":1353},"can-a-lambda-function-contain-multiple-lines","Can a lambda function contain multiple lines?",[14,1356,1357],{},"No. A lambda is limited to one expression.",[520,1359,1361],{"id":1360},"should-i-always-use-lambda-instead-of-def","Should I always use lambda instead of def?",[14,1363,1364,1365,1367],{},"No. Use ",[21,1366,359],{}," when the function is longer, reused, or needs to be easier to read.",[114,1369,1371],{"id":1370},"see-also","See also",[122,1373,1374,1379,1383,1388,1393,1398,1404,1410],{},[125,1375,1376],{},[203,1377,1378],{"href":510},"Lambda functions in Python explained",[125,1380,1381],{},[203,1382,507],{"href":506},[125,1384,1385],{},[203,1386,1387],{"href":205},"What is a function in Python?",[125,1389,1390],{},[203,1391,1392],{"href":344},"What is a parameter in Python?",[125,1394,1395],{},[203,1396,1397],{"href":348},"What is an argument in Python?",[125,1399,1400],{},[203,1401,723,1402,726],{"href":722},[21,1403,526],{},[125,1405,1406],{},[203,1407,723,1408,726],{"href":859},[21,1409,23],{},[125,1411,1412],{},[203,1413,723,1414,726],{"href":992},[21,1415,27],{},[1417,1418,1419],"style",{},"html pre.shiki code .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sbsja, html code.shiki .sbsja{--shiki-light:#9C3EDA;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sFwrP, html code.shiki .sFwrP{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#24292E;--shiki-default-font-style:inherit;--shiki-dark:#E1E4E8;--shiki-dark-font-style:inherit}html pre.shiki code .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .sGLFI, html code.shiki .sGLFI{--shiki-light:#6182B8;--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .sVHd0, html code.shiki .sVHd0{--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#D73A49;--shiki-default-font-style:inherit;--shiki-dark:#F97583;--shiki-dark-font-style:inherit}html pre.shiki code .sjJ54, html code.shiki .sjJ54{--shiki-light:#39ADB5;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .s_sjI, html code.shiki .s_sjI{--shiki-light:#91B859;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .s99_P, html code.shiki .s99_P{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#E36209;--shiki-default-font-style:inherit;--shiki-dark:#FFAB70;--shiki-dark-font-style:inherit}html pre.shiki code .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":38,"searchDepth":77,"depth":77,"links":1421},[1422,1423,1424,1425,1433,1434,1435,1436,1442],{"id":116,"depth":77,"text":117},{"id":210,"depth":77,"text":211},{"id":352,"depth":77,"text":353},{"id":514,"depth":77,"text":515,"children":1426},[1427,1429,1431],{"id":522,"depth":399,"text":1428},"With sorted()",{"id":734,"depth":399,"text":1430},"With map()",{"id":864,"depth":399,"text":1432},"With filter()",{"id":997,"depth":77,"text":998},{"id":1041,"depth":77,"text":1042},{"id":1173,"depth":77,"text":1174},{"id":1286,"depth":77,"text":1287,"children":1437},[1438,1439,1440,1441],{"id":1290,"depth":399,"text":1291},{"id":1297,"depth":399,"text":1298},{"id":1353,"depth":399,"text":1354},{"id":1360,"depth":399,"text":1361},{"id":1370,"depth":77,"text":1371},"Master what is a lambda function in python in our comprehensive Python beginner guide.","md",{},"\u002Fglossary\u002Fwhat-is-a-lambda-function-in-python",{"title":5,"description":1443},"glossary\u002Fwhat-is-a-lambda-function-in-python","1duFT-oZBMjE43zr-Wd_Oj61ufSUSXHoWaFpR-MM4hc",1777585468116]