[{"data":1,"prerenderedAt":814},["ShallowReactive",2],{"blog-\u002Fblog\u002Fpython-lambdas-higher-order-functions-explained":3},{"id":4,"title":5,"body":6,"description":800,"difficulty":801,"extension":802,"framework":803,"frameworkSlug":43,"meta":804,"navigation":100,"order":104,"path":805,"qaPath":806,"seo":807,"stem":808,"subtopic":809,"topic":810,"topicSlug":811,"updated":812,"__hash__":813},"blog\u002Fblog\u002Fpython-lambdas-higher-order-functions-explained.md","Python Lambdas and Higher-Order Functions Explained — key=, First-Class Functions",{"type":7,"value":8,"toc":790},"minimark",[9,14,28,32,38,119,129,133,153,215,219,239,298,305,309,327,379,382,386,404,564,574,614,618,621,733,751,755,786],[10,11,13],"h2",{"id":12},"python-lambdas-explained","Python lambdas, explained",[15,16,17,18,22,23,27],"p",{},"A lambda is a small anonymous function written inline. It's not more powerful than a normal\nfunction — it's ",[19,20,21],"em",{},"more limited"," — but it shines in one place: passing a short throwaway\nfunction as an argument, especially as a ",[24,25,26],"code",{},"key",". This guide covers what lambdas can and can't\ndo, and the higher-order functions that consume them.",[10,29,31],{"id":30},"what-a-lambda-is","What a lambda is",[15,33,34,37],{},[24,35,36],{},"lambda"," builds a function object from a single expression. These two are equivalent:",[39,40,45],"pre",{"className":41,"code":42,"language":43,"meta":44,"style":44},"language-python shiki shiki-themes github-light github-dark","square = lambda x: x * x\ndef square(x): return x * x\n\nsquare(5)    # 25\n","python","",[24,46,47,72,95,102],{"__ignoreMap":44},[48,49,52,56,60,63,66,69],"span",{"class":50,"line":51},"line",1,[48,53,55],{"class":54},"sVt8B","square ",[48,57,59],{"class":58},"szBVR","=",[48,61,62],{"class":58}," lambda",[48,64,65],{"class":54}," x: x ",[48,67,68],{"class":58},"*",[48,70,71],{"class":54}," x\n",[48,73,75,78,82,85,88,91,93],{"class":50,"line":74},2,[48,76,77],{"class":58},"def",[48,79,81],{"class":80},"sScJk"," square",[48,83,84],{"class":54},"(x): ",[48,86,87],{"class":58},"return",[48,89,90],{"class":54}," x ",[48,92,68],{"class":58},[48,94,71],{"class":54},[48,96,98],{"class":50,"line":97},3,[48,99,101],{"emptyLinePlaceholder":100},true,"\n",[48,103,105,108,112,115],{"class":50,"line":104},4,[48,106,107],{"class":54},"square(",[48,109,111],{"class":110},"sj4cs","5",[48,113,114],{"class":54},")    ",[48,116,118],{"class":117},"sJ8bj","# 25\n",[15,120,121,122,125,126,128],{},"The syntax is ",[24,123,124],{},"lambda args: expression",". The expression's value is returned automatically —\nthere's no ",[24,127,87],{}," keyword.",[10,130,132],{"id":131},"the-limitations-of-lambdas","The limitations of lambdas",[15,134,135,136,140,141,144,145,148,149,152],{},"A lambda is restricted to a ",[137,138,139],"strong",{},"single expression",". It cannot contain statements —\nno assignments, no ",[24,142,143],{},"if","\u002F",[24,146,147],{},"for"," blocks, no ",[24,150,151],{},"try",", no multiple lines:",[39,154,156],{"className":41,"code":155,"language":43,"meta":44,"style":44},"lambda x: x * 2                      # fine\nlambda x: x if x > 0 else -x         # fine (conditional EXPRESSION is allowed)\nlambda x: return x                   # SyntaxError — statements not allowed\n",[24,157,158,172,200],{"__ignoreMap":44},[48,159,160,162,164,166,169],{"class":50,"line":51},[48,161,36],{"class":58},[48,163,65],{"class":54},[48,165,68],{"class":58},[48,167,168],{"class":110}," 2",[48,170,171],{"class":117},"                      # fine\n",[48,173,174,176,178,180,182,185,188,191,194,197],{"class":50,"line":74},[48,175,36],{"class":58},[48,177,65],{"class":54},[48,179,143],{"class":58},[48,181,90],{"class":54},[48,183,184],{"class":58},">",[48,186,187],{"class":110}," 0",[48,189,190],{"class":58}," else",[48,192,193],{"class":58}," -",[48,195,196],{"class":54},"x         ",[48,198,199],{"class":117},"# fine (conditional EXPRESSION is allowed)\n",[48,201,202,204,207,209,212],{"class":50,"line":97},[48,203,36],{"class":58},[48,205,206],{"class":54}," x: ",[48,208,87],{"class":58},[48,210,211],{"class":54}," x                   ",[48,213,214],{"class":117},"# SyntaxError — statements not allowed\n",[10,216,218],{"id":217},"lambda-vs-def","lambda vs def",[15,220,221,222,224,225,228,229,231,232,234,235,238],{},"Because a lambda's body is one expression, use ",[24,223,77],{}," for anything with real logic. Critically,\n",[137,226,227],{},"don't assign a lambda to a name"," — that's exactly what ",[24,230,77],{}," is for, and ",[24,233,77],{}," gives the\nfunction a proper ",[24,236,237],{},"__name__"," for tracebacks:",[39,240,242],{"className":41,"code":241,"language":43,"meta":44,"style":44},"# Un-Pythonic — a named lambda:\nf = lambda x: x + 1\n\n# Preferred:\ndef f(x):\n    return x + 1\n",[24,243,244,249,266,270,275,286],{"__ignoreMap":44},[48,245,246],{"class":50,"line":51},[48,247,248],{"class":117},"# Un-Pythonic — a named lambda:\n",[48,250,251,254,256,258,260,263],{"class":50,"line":74},[48,252,253],{"class":54},"f ",[48,255,59],{"class":58},[48,257,62],{"class":58},[48,259,65],{"class":54},[48,261,262],{"class":58},"+",[48,264,265],{"class":110}," 1\n",[48,267,268],{"class":50,"line":97},[48,269,101],{"emptyLinePlaceholder":100},[48,271,272],{"class":50,"line":104},[48,273,274],{"class":117},"# Preferred:\n",[48,276,278,280,283],{"class":50,"line":277},5,[48,279,77],{"class":58},[48,281,282],{"class":80}," f",[48,284,285],{"class":54},"(x):\n",[48,287,289,292,294,296],{"class":50,"line":288},6,[48,290,291],{"class":58},"    return",[48,293,90],{"class":54},[48,295,262],{"class":58},[48,297,265],{"class":110},[15,299,300,301,304],{},"Use a lambda only when the function is ",[137,302,303],{},"anonymous and passed inline",".",[10,306,308],{"id":307},"higher-order-functions","Higher-order functions",[15,310,311,312,315,316,319,320,319,323,326],{},"A higher-order function is one that ",[137,313,314],{},"takes a function as an argument or returns one",".\n",[24,317,318],{},"map",", ",[24,321,322],{},"filter",[24,324,325],{},"sorted",", and decorators are all higher-order:",[39,328,330],{"className":41,"code":329,"language":43,"meta":44,"style":44},"def apply_twice(fn, x):\n    return fn(fn(x))\n\napply_twice(lambda n: n + 3, 10)   # 16\n",[24,331,332,342,349,353],{"__ignoreMap":44},[48,333,334,336,339],{"class":50,"line":51},[48,335,77],{"class":58},[48,337,338],{"class":80}," apply_twice",[48,340,341],{"class":54},"(fn, x):\n",[48,343,344,346],{"class":50,"line":74},[48,345,291],{"class":58},[48,347,348],{"class":54}," fn(fn(x))\n",[48,350,351],{"class":50,"line":97},[48,352,101],{"emptyLinePlaceholder":100},[48,354,355,358,360,363,365,368,370,373,376],{"class":50,"line":104},[48,356,357],{"class":54},"apply_twice(",[48,359,36],{"class":58},[48,361,362],{"class":54}," n: n ",[48,364,262],{"class":58},[48,366,367],{"class":110}," 3",[48,369,319],{"class":54},[48,371,372],{"class":110},"10",[48,374,375],{"class":54},")   ",[48,377,378],{"class":117},"# 16\n",[15,380,381],{},"This is possible because functions are first-class (next section).",[10,383,385],{"id":384},"the-key-argument","The key argument",[15,387,388,389,391,392,319,394,397,398,315,401,403],{},"The most useful place for a lambda is the ",[24,390,26],{}," parameter of ",[24,393,325],{},[24,395,396],{},"max",", and ",[24,399,400],{},"min",[24,402,26],{}," is a function applied to each element to decide the comparison value:",[39,405,407],{"className":41,"code":406,"language":43,"meta":44,"style":44},"words = [\"banana\", \"kiwi\", \"apple\"]\nsorted(words, key=len)                  # ['kiwi', 'apple', 'banana']\nmax(words, key=len)                     # 'banana'\n\npeople = [(\"Ada\", 36), (\"Alan\", 41)]\nsorted(people, key=lambda p: p[1])      # sort by age\nsorted(people, key=lambda p: p[1], reverse=True)\n",[24,408,409,436,457,475,479,511,535],{"__ignoreMap":44},[48,410,411,414,416,419,423,425,428,430,433],{"class":50,"line":51},[48,412,413],{"class":54},"words ",[48,415,59],{"class":58},[48,417,418],{"class":54}," [",[48,420,422],{"class":421},"sZZnC","\"banana\"",[48,424,319],{"class":54},[48,426,427],{"class":421},"\"kiwi\"",[48,429,319],{"class":54},[48,431,432],{"class":421},"\"apple\"",[48,434,435],{"class":54},"]\n",[48,437,438,440,443,446,448,451,454],{"class":50,"line":74},[48,439,325],{"class":110},[48,441,442],{"class":54},"(words, ",[48,444,26],{"class":445},"s4XuR",[48,447,59],{"class":58},[48,449,450],{"class":110},"len",[48,452,453],{"class":54},")                  ",[48,455,456],{"class":117},"# ['kiwi', 'apple', 'banana']\n",[48,458,459,461,463,465,467,469,472],{"class":50,"line":97},[48,460,396],{"class":110},[48,462,442],{"class":54},[48,464,26],{"class":445},[48,466,59],{"class":58},[48,468,450],{"class":110},[48,470,471],{"class":54},")                     ",[48,473,474],{"class":117},"# 'banana'\n",[48,476,477],{"class":50,"line":104},[48,478,101],{"emptyLinePlaceholder":100},[48,480,481,484,486,489,492,494,497,500,503,505,508],{"class":50,"line":277},[48,482,483],{"class":54},"people ",[48,485,59],{"class":58},[48,487,488],{"class":54}," [(",[48,490,491],{"class":421},"\"Ada\"",[48,493,319],{"class":54},[48,495,496],{"class":110},"36",[48,498,499],{"class":54},"), (",[48,501,502],{"class":421},"\"Alan\"",[48,504,319],{"class":54},[48,506,507],{"class":110},"41",[48,509,510],{"class":54},")]\n",[48,512,513,515,518,520,523,526,529,532],{"class":50,"line":288},[48,514,325],{"class":110},[48,516,517],{"class":54},"(people, ",[48,519,26],{"class":445},[48,521,522],{"class":58},"=lambda",[48,524,525],{"class":54}," p: p[",[48,527,528],{"class":110},"1",[48,530,531],{"class":54},"])      ",[48,533,534],{"class":117},"# sort by age\n",[48,536,538,540,542,544,546,548,550,553,556,558,561],{"class":50,"line":537},7,[48,539,325],{"class":110},[48,541,517],{"class":54},[48,543,26],{"class":445},[48,545,522],{"class":58},[48,547,525],{"class":54},[48,549,528],{"class":110},[48,551,552],{"class":54},"], ",[48,554,555],{"class":445},"reverse",[48,557,59],{"class":58},[48,559,560],{"class":110},"True",[48,562,563],{"class":54},")\n",[15,565,566,567,144,570,573],{},"For attribute or item access, ",[24,568,569],{},"operator.itemgetter",[24,571,572],{},"attrgetter"," are faster and clearer than\na lambda:",[39,575,577],{"className":41,"code":576,"language":43,"meta":44,"style":44},"from operator import itemgetter\nsorted(people, key=itemgetter(1))       # same as the lambda above\n",[24,578,579,593],{"__ignoreMap":44},[48,580,581,584,587,590],{"class":50,"line":51},[48,582,583],{"class":58},"from",[48,585,586],{"class":54}," operator ",[48,588,589],{"class":58},"import",[48,591,592],{"class":54}," itemgetter\n",[48,594,595,597,599,601,603,606,608,611],{"class":50,"line":74},[48,596,325],{"class":110},[48,598,517],{"class":54},[48,600,26],{"class":445},[48,602,59],{"class":58},[48,604,605],{"class":54},"itemgetter(",[48,607,528],{"class":110},[48,609,610],{"class":54},"))       ",[48,612,613],{"class":117},"# same as the lambda above\n",[10,615,617],{"id":616},"functions-are-first-class-objects","Functions are first-class objects",[15,619,620],{},"\"First-class\" means functions are ordinary objects: you can assign them to variables, store\nthem in lists or dicts, pass them as arguments, and return them. This is what makes lambdas,\nhigher-order functions, and decorators possible:",[39,622,624],{"className":41,"code":623,"language":43,"meta":44,"style":44},"ops = {\"+\": lambda a, b: a + b, \"-\": lambda a, b: a - b}\nops[\"+\"](2, 3)      # 5 — a function stored in a dict, called by key\n\ndef greet():\n    return \"hi\"\ng = greet           # assign the function object itself (no parentheses)\ng()                 # 'hi'\n",[24,625,626,667,691,695,705,712,725],{"__ignoreMap":44},[48,627,628,631,633,636,639,642,644,647,649,652,655,657,659,661,664],{"class":50,"line":51},[48,629,630],{"class":54},"ops ",[48,632,59],{"class":58},[48,634,635],{"class":54}," {",[48,637,638],{"class":421},"\"+\"",[48,640,641],{"class":54},": ",[48,643,36],{"class":58},[48,645,646],{"class":54}," a, b: a ",[48,648,262],{"class":58},[48,650,651],{"class":54}," b, ",[48,653,654],{"class":421},"\"-\"",[48,656,641],{"class":54},[48,658,36],{"class":58},[48,660,646],{"class":54},[48,662,663],{"class":58},"-",[48,665,666],{"class":54}," b}\n",[48,668,669,672,674,677,680,682,685,688],{"class":50,"line":74},[48,670,671],{"class":54},"ops[",[48,673,638],{"class":421},[48,675,676],{"class":54},"](",[48,678,679],{"class":110},"2",[48,681,319],{"class":54},[48,683,684],{"class":110},"3",[48,686,687],{"class":54},")      ",[48,689,690],{"class":117},"# 5 — a function stored in a dict, called by key\n",[48,692,693],{"class":50,"line":97},[48,694,101],{"emptyLinePlaceholder":100},[48,696,697,699,702],{"class":50,"line":104},[48,698,77],{"class":58},[48,700,701],{"class":80}," greet",[48,703,704],{"class":54},"():\n",[48,706,707,709],{"class":50,"line":277},[48,708,291],{"class":58},[48,710,711],{"class":421}," \"hi\"\n",[48,713,714,717,719,722],{"class":50,"line":288},[48,715,716],{"class":54},"g ",[48,718,59],{"class":58},[48,720,721],{"class":54}," greet           ",[48,723,724],{"class":117},"# assign the function object itself (no parentheses)\n",[48,726,727,730],{"class":50,"line":537},[48,728,729],{"class":54},"g()                 ",[48,731,732],{"class":117},"# 'hi'\n",[15,734,735,736,739,740,743,744,747,748,750],{},"Note ",[24,737,738],{},"greet"," is the function object; ",[24,741,742],{},"greet()"," ",[19,745,746],{},"calls"," it. Passing ",[24,749,738],{}," (no parens) hands\nthe function around for someone else to call.",[10,752,754],{"id":753},"recap","Recap",[15,756,757,758,761,762,764,765,768,769,771,772,144,774,144,776,778,779,781,782,785],{},"A lambda is an anonymous, single-",[137,759,760],{},"expression"," function — handy inline, but it can't hold\nstatements, so use ",[24,763,77],{}," for anything substantial and never bind a lambda to a name. A\n",[137,766,767],{},"higher-order function"," takes or returns functions, the prime example being the ",[24,770,26],{},"\nargument to ",[24,773,325],{},[24,775,396],{},[24,777,400],{}," (or ",[24,780,569],{}," for plain field access). All of\nthis rests on functions being ",[137,783,784],{},"first-class objects"," you can pass, store, and return like\nany other value.",[787,788,789],"style",{},"html pre.shiki code .sVt8B, html code.shiki .sVt8B{--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .szBVR, html code.shiki .szBVR{--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sScJk, html code.shiki .sScJk{--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .sj4cs, html code.shiki .sj4cs{--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sJ8bj, html code.shiki .sJ8bj{--shiki-default:#6A737D;--shiki-dark:#6A737D}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 .sZZnC, html code.shiki .sZZnC{--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .s4XuR, html code.shiki .s4XuR{--shiki-default:#E36209;--shiki-dark:#FFAB70}",{"title":44,"searchDepth":74,"depth":74,"links":791},[792,793,794,795,796,797,798,799],{"id":12,"depth":74,"text":13},{"id":30,"depth":74,"text":31},{"id":131,"depth":74,"text":132},{"id":217,"depth":74,"text":218},{"id":307,"depth":74,"text":308},{"id":384,"depth":74,"text":385},{"id":616,"depth":74,"text":617},{"id":753,"depth":74,"text":754},"What Python lambdas are and their limits, lambda vs def, higher-order functions, how the key argument powers sorted\u002Fmax\u002Fmin, and what first-class functions mean.","medium","md","Python",{},"\u002Fblog\u002Fpython-lambdas-higher-order-functions-explained","\u002Fpython\u002Ffunctions\u002Flambdas",{"title":5,"description":800},"blog\u002Fpython-lambdas-higher-order-functions-explained","Lambdas & Higher-Order Functions","Functions","functions","2026-06-19","Ls6F5ce_s0laarO-Uw9cARR_LPAKFIcqjQ1G2axT3Lc",1782244093848]