[{"data":1,"prerenderedAt":835},["ShallowReactive",2],{"blog-\u002Fblog\u002Fpython-map-filter-reduce-explained":3},{"id":4,"title":5,"body":6,"description":821,"difficulty":822,"extension":823,"framework":824,"frameworkSlug":59,"meta":825,"navigation":351,"order":103,"path":826,"qaPath":827,"seo":828,"stem":829,"subtopic":830,"topic":831,"topicSlug":832,"updated":833,"__hash__":834},"blog\u002Fblog\u002Fpython-map-filter-reduce-explained.md","Python map, filter & reduce Explained — Functional Transforms vs Comprehensions",{"type":7,"value":8,"toc":811},"minimark",[9,14,35,39,54,143,148,206,212,216,230,308,314,318,327,432,439,443,452,584,601,605,610,677,690,694,723,760,764,807],[10,11,13],"h2",{"id":12},"python-map-filter-reduce-explained","Python map, filter & reduce, explained",[15,16,17,21,22,25,26,29,30,34],"p",{},[18,19,20],"code",{},"map",", ",[18,23,24],{},"filter",", and ",[18,27,28],{},"reduce"," are the classic functional trio for transforming, selecting,\nand folding sequences. Python supports all three, but it also has comprehensions — so the\nreal interview question is usually ",[31,32,33],"em",{},"when"," each is the right tool, not just how they work.",[10,36,38],{"id":37},"map-apply-a-function-to-every-item","map — apply a function to every item",[15,40,41,44,45,49,50,53],{},[18,42,43],{},"map(func, iterable)"," returns a ",[46,47,48],"strong",{},"lazy iterator"," that calls ",[18,51,52],{},"func"," on each element. Nothing\nruns until you consume it.",[55,56,61],"pre",{"className":57,"code":58,"language":59,"meta":60,"style":60},"language-python shiki shiki-themes github-light github-dark","nums = [1, 2, 3, 4]\nsquared = map(lambda x: x * x, nums)   # a map object — nothing computed yet\nlist(squared)                          # [1, 4, 9, 16]\n","python","",[18,62,63,101,131],{"__ignoreMap":60},[64,65,68,72,76,79,83,85,88,90,93,95,98],"span",{"class":66,"line":67},"line",1,[64,69,71],{"class":70},"sVt8B","nums ",[64,73,75],{"class":74},"szBVR","=",[64,77,78],{"class":70}," [",[64,80,82],{"class":81},"sj4cs","1",[64,84,21],{"class":70},[64,86,87],{"class":81},"2",[64,89,21],{"class":70},[64,91,92],{"class":81},"3",[64,94,21],{"class":70},[64,96,97],{"class":81},"4",[64,99,100],{"class":70},"]\n",[64,102,104,107,109,112,115,118,121,124,127],{"class":66,"line":103},2,[64,105,106],{"class":70},"squared ",[64,108,75],{"class":74},[64,110,111],{"class":81}," map",[64,113,114],{"class":70},"(",[64,116,117],{"class":74},"lambda",[64,119,120],{"class":70}," x: x ",[64,122,123],{"class":74},"*",[64,125,126],{"class":70}," x, nums)   ",[64,128,130],{"class":129},"sJ8bj","# a map object — nothing computed yet\n",[64,132,134,137,140],{"class":66,"line":133},3,[64,135,136],{"class":81},"list",[64,138,139],{"class":70},"(squared)                          ",[64,141,142],{"class":129},"# [1, 4, 9, 16]\n",[15,144,145,147],{},[18,146,20],{}," can take multiple iterables, applying the function across them in parallel — handy and\nconcise:",[55,149,151],{"className":57,"code":150,"language":59,"meta":60,"style":60},"list(map(lambda a, b: a + b, [1, 2, 3], [10, 20, 30]))   # [11, 22, 33]\n",[18,152,153],{"__ignoreMap":60},[64,154,155,157,159,161,163,165,168,171,174,176,178,180,182,184,187,190,192,195,197,200,203],{"class":66,"line":67},[64,156,136],{"class":81},[64,158,114],{"class":70},[64,160,20],{"class":81},[64,162,114],{"class":70},[64,164,117],{"class":74},[64,166,167],{"class":70}," a, b: a ",[64,169,170],{"class":74},"+",[64,172,173],{"class":70}," b, [",[64,175,82],{"class":81},[64,177,21],{"class":70},[64,179,87],{"class":81},[64,181,21],{"class":70},[64,183,92],{"class":81},[64,185,186],{"class":70},"], [",[64,188,189],{"class":81},"10",[64,191,21],{"class":70},[64,193,194],{"class":81},"20",[64,196,21],{"class":70},[64,198,199],{"class":81},"30",[64,201,202],{"class":70},"]))   ",[64,204,205],{"class":129},"# [11, 22, 33]\n",[15,207,208,209,211],{},"It stops at the shortest iterable. Because it's lazy, ",[18,210,20],{}," over a huge sequence uses\nconstant memory.",[10,213,215],{"id":214},"filter-keep-items-that-pass-a-test","filter — keep items that pass a test",[15,217,218,221,222,225,226,229],{},[18,219,220],{},"filter(predicate, iterable)"," yields only the elements for which ",[18,223,224],{},"predicate"," returns truthy.\nPassing ",[18,227,228],{},"None"," as the predicate keeps the truthy items.",[55,231,233],{"className":57,"code":232,"language":59,"meta":60,"style":60},"nums = [0, 1, 2, 0, 3]\nlist(filter(lambda x: x > 1, nums))    # [2, 3]\nlist(filter(None, nums))               # [1, 2, 3] — drops falsy values\n",[18,234,235,264,290],{"__ignoreMap":60},[64,236,237,239,241,243,246,248,250,252,254,256,258,260,262],{"class":66,"line":67},[64,238,71],{"class":70},[64,240,75],{"class":74},[64,242,78],{"class":70},[64,244,245],{"class":81},"0",[64,247,21],{"class":70},[64,249,82],{"class":81},[64,251,21],{"class":70},[64,253,87],{"class":81},[64,255,21],{"class":70},[64,257,245],{"class":81},[64,259,21],{"class":70},[64,261,92],{"class":81},[64,263,100],{"class":70},[64,265,266,268,270,272,274,276,278,281,284,287],{"class":66,"line":103},[64,267,136],{"class":81},[64,269,114],{"class":70},[64,271,24],{"class":81},[64,273,114],{"class":70},[64,275,117],{"class":74},[64,277,120],{"class":70},[64,279,280],{"class":74},">",[64,282,283],{"class":81}," 1",[64,285,286],{"class":70},", nums))    ",[64,288,289],{"class":129},"# [2, 3]\n",[64,291,292,294,296,298,300,302,305],{"class":66,"line":133},[64,293,136],{"class":81},[64,295,114],{"class":70},[64,297,24],{"class":81},[64,299,114],{"class":70},[64,301,228],{"class":81},[64,303,304],{"class":70},", nums))               ",[64,306,307],{"class":129},"# [1, 2, 3] — drops falsy values\n",[15,309,310,311,313],{},"Like ",[18,312,20],{},", it returns a lazy iterator, so it composes cheaply into pipelines.",[10,315,317],{"id":316},"reduce-fold-to-a-single-value","reduce — fold to a single value",[15,319,320,322,323,326],{},[18,321,28],{}," lives in ",[18,324,325],{},"functools"," (it was moved out of builtins in Python 3 because it's rarely\nthe clearest option). It repeatedly applies a two-argument function, carrying an accumulator.",[55,328,330],{"className":57,"code":329,"language":59,"meta":60,"style":60},"from functools import reduce\n\nreduce(lambda acc, x: acc + x, [1, 2, 3, 4], 0)        # 10\nreduce(lambda acc, x: acc * x, [1, 2, 3, 4], 1)        # 24\n",[18,331,332,347,353,394],{"__ignoreMap":60},[64,333,334,337,340,343],{"class":66,"line":67},[64,335,336],{"class":74},"from",[64,338,339],{"class":70}," functools ",[64,341,342],{"class":74},"import",[64,344,346],{"class":345},"s4XuR"," reduce\n",[64,348,349],{"class":66,"line":103},[64,350,352],{"emptyLinePlaceholder":351},true,"\n",[64,354,355,357,359,361,364,366,369,371,373,375,377,379,381,383,386,388,391],{"class":66,"line":133},[64,356,28],{"class":345},[64,358,114],{"class":70},[64,360,117],{"class":74},[64,362,363],{"class":70}," acc, x: acc ",[64,365,170],{"class":74},[64,367,368],{"class":70}," x, [",[64,370,82],{"class":81},[64,372,21],{"class":70},[64,374,87],{"class":81},[64,376,21],{"class":70},[64,378,92],{"class":81},[64,380,21],{"class":70},[64,382,97],{"class":81},[64,384,385],{"class":70},"], ",[64,387,245],{"class":81},[64,389,390],{"class":70},")        ",[64,392,393],{"class":129},"# 10\n",[64,395,397,399,401,403,405,407,409,411,413,415,417,419,421,423,425,427,429],{"class":66,"line":396},4,[64,398,28],{"class":345},[64,400,114],{"class":70},[64,402,117],{"class":74},[64,404,363],{"class":70},[64,406,123],{"class":74},[64,408,368],{"class":70},[64,410,82],{"class":81},[64,412,21],{"class":70},[64,414,87],{"class":81},[64,416,21],{"class":70},[64,418,92],{"class":81},[64,420,21],{"class":70},[64,422,97],{"class":81},[64,424,385],{"class":70},[64,426,82],{"class":81},[64,428,390],{"class":70},[64,430,431],{"class":129},"# 24\n",[15,433,434,435,438],{},"The optional initial value (last argument) is also the result for an empty iterable, which\navoids a ",[18,436,437],{},"TypeError",".",[10,440,442],{"id":441},"the-pythonic-alternative-comprehensions","The Pythonic alternative: comprehensions",[15,444,445,446,448,449,451],{},"For ",[18,447,20],{}," and ",[18,450,24],{},", a list\u002Fgenerator comprehension is usually more readable — and it\navoids a lambda entirely.",[55,453,455],{"className":57,"code":454,"language":59,"meta":60,"style":60},"nums = [1, 2, 3, 4]\n\n# map + filter\n[x * x for x in nums if x % 2 == 0]            # [4, 16]\n\n# vs the functional version\nlist(map(lambda x: x * x, filter(lambda x: x % 2 == 0, nums)))\n",[18,456,457,481,485,490,534,539,545],{"__ignoreMap":60},[64,458,459,461,463,465,467,469,471,473,475,477,479],{"class":66,"line":67},[64,460,71],{"class":70},[64,462,75],{"class":74},[64,464,78],{"class":70},[64,466,82],{"class":81},[64,468,21],{"class":70},[64,470,87],{"class":81},[64,472,21],{"class":70},[64,474,92],{"class":81},[64,476,21],{"class":70},[64,478,97],{"class":81},[64,480,100],{"class":70},[64,482,483],{"class":66,"line":103},[64,484,352],{"emptyLinePlaceholder":351},[64,486,487],{"class":66,"line":133},[64,488,489],{"class":129},"# map + filter\n",[64,491,492,495,497,500,503,505,508,511,514,516,519,522,525,528,531],{"class":66,"line":396},[64,493,494],{"class":70},"[x ",[64,496,123],{"class":74},[64,498,499],{"class":70}," x ",[64,501,502],{"class":74},"for",[64,504,499],{"class":70},[64,506,507],{"class":74},"in",[64,509,510],{"class":70}," nums ",[64,512,513],{"class":74},"if",[64,515,499],{"class":70},[64,517,518],{"class":74},"%",[64,520,521],{"class":81}," 2",[64,523,524],{"class":74}," ==",[64,526,527],{"class":81}," 0",[64,529,530],{"class":70},"]            ",[64,532,533],{"class":129},"# [4, 16]\n",[64,535,537],{"class":66,"line":536},5,[64,538,352],{"emptyLinePlaceholder":351},[64,540,542],{"class":66,"line":541},6,[64,543,544],{"class":129},"# vs the functional version\n",[64,546,548,550,552,554,556,558,560,562,565,567,569,571,573,575,577,579,581],{"class":66,"line":547},7,[64,549,136],{"class":81},[64,551,114],{"class":70},[64,553,20],{"class":81},[64,555,114],{"class":70},[64,557,117],{"class":74},[64,559,120],{"class":70},[64,561,123],{"class":74},[64,563,564],{"class":70}," x, ",[64,566,24],{"class":81},[64,568,114],{"class":70},[64,570,117],{"class":74},[64,572,120],{"class":70},[64,574,518],{"class":74},[64,576,521],{"class":81},[64,578,524],{"class":74},[64,580,527],{"class":81},[64,582,583],{"class":70},", nums)))\n",[15,585,586,587,589,590,593,594,597,598,600],{},"The comprehension reads left-to-right and needs no ",[18,588,117],{},". Use a ",[46,591,592],{},"generator"," expression\n",[18,595,596],{},"(x*x for x in nums)"," when you want laziness like ",[18,599,20],{}," gives.",[10,602,604],{"id":603},"when-mapfilter-still-win","When map\u002Ffilter still win",[15,606,607,609],{},[18,608,20],{}," is genuinely nicer when you already have a named function — no lambda, no rebuilding:",[55,611,613],{"className":57,"code":612,"language":59,"meta":60,"style":60},"names = [\"  alice \", \"BOB  \"]\nlist(map(str.strip, names))            # ['alice', 'BOB  '.strip()...] clean and direct\nclean = [name.strip() for name in names]   # comprehension equivalent\n",[18,614,615,635,654],{"__ignoreMap":60},[64,616,617,620,622,624,628,630,633],{"class":66,"line":67},[64,618,619],{"class":70},"names ",[64,621,75],{"class":74},[64,623,78],{"class":70},[64,625,627],{"class":626},"sZZnC","\"  alice \"",[64,629,21],{"class":70},[64,631,632],{"class":626},"\"BOB  \"",[64,634,100],{"class":70},[64,636,637,639,641,643,645,648,651],{"class":66,"line":103},[64,638,136],{"class":81},[64,640,114],{"class":70},[64,642,20],{"class":81},[64,644,114],{"class":70},[64,646,647],{"class":81},"str",[64,649,650],{"class":70},".strip, names))            ",[64,652,653],{"class":129},"# ['alice', 'BOB  '.strip()...] clean and direct\n",[64,655,656,659,661,664,666,669,671,674],{"class":66,"line":133},[64,657,658],{"class":70},"clean ",[64,660,75],{"class":74},[64,662,663],{"class":70}," [name.strip() ",[64,665,502],{"class":74},[64,667,668],{"class":70}," name ",[64,670,507],{"class":74},[64,672,673],{"class":70}," names]   ",[64,675,676],{"class":129},"# comprehension equivalent\n",[15,678,679,682,683,686,687,689],{},[18,680,681],{},"map(int, tokens)"," or ",[18,684,685],{},"map(str.upper, words)"," are crisp. The moment you need a ",[18,688,117],{},",\na comprehension is usually clearer.",[10,691,693],{"id":692},"reduce-vs-built-ins-and-loops","reduce vs built-ins and loops",[15,695,696,697,21,700,21,703,21,706,21,709,712,713,716,717,719,720,722],{},"Most \"reduce\" tasks have a dedicated built-in: ",[18,698,699],{},"sum",[18,701,702],{},"min",[18,704,705],{},"max",[18,707,708],{},"any",[18,710,711],{},"all",",\n",[18,714,715],{},"\"\".join",". Reach for those first — they're faster and clearer. Save ",[18,718,28],{}," for genuine\ncustom folds, and even then a plain ",[18,721,502],{}," loop is often more readable to teammates.",[55,724,726],{"className":57,"code":725,"language":59,"meta":60,"style":60},"# Don't:\nreduce(lambda a, b: a + b, nums)\n# Do:\nsum(nums)\n",[18,727,728,733,748,753],{"__ignoreMap":60},[64,729,730],{"class":66,"line":67},[64,731,732],{"class":129},"# Don't:\n",[64,734,735,737,739,741,743,745],{"class":66,"line":103},[64,736,28],{"class":345},[64,738,114],{"class":70},[64,740,117],{"class":74},[64,742,167],{"class":70},[64,744,170],{"class":74},[64,746,747],{"class":70}," b, nums)\n",[64,749,750],{"class":66,"line":133},[64,751,752],{"class":129},"# Do:\n",[64,754,755,757],{"class":66,"line":396},[64,756,699],{"class":81},[64,758,759],{"class":70},"(nums)\n",[10,761,763],{"id":762},"recap","Recap",[15,765,766,768,769,771,772,775,776,778,779,781,782,785,786,788,789,791,792,795,796,712,798,25,800,803,804,806],{},[18,767,20],{}," applies a function to every item, ",[18,770,24],{}," keeps the ones passing a predicate, and\nboth return ",[46,773,774],{},"lazy iterators","; ",[18,777,28],{}," (in ",[18,780,325],{},") folds a sequence to one value with\nan accumulator and optional initial value. In modern Python, ",[46,783,784],{},"comprehensions"," and\ngenerator expressions usually replace ",[18,787,20],{},"\u002F",[18,790,24],{}," more readably — but ",[18,793,794],{},"map(func, ...)","\nwith an existing named function stays elegant. For folds, prefer built-ins like ",[18,797,699],{},[18,799,705],{},[18,801,802],{},"join"," over ",[18,805,28],{}," whenever one exists.",[808,809,810],"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 .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 .s4XuR, html code.shiki .s4XuR{--shiki-default:#E36209;--shiki-dark:#FFAB70}html pre.shiki code .sZZnC, html code.shiki .sZZnC{--shiki-default:#032F62;--shiki-dark:#9ECBFF}",{"title":60,"searchDepth":103,"depth":103,"links":812},[813,814,815,816,817,818,819,820],{"id":12,"depth":103,"text":13},{"id":37,"depth":103,"text":38},{"id":214,"depth":103,"text":215},{"id":316,"depth":103,"text":317},{"id":441,"depth":103,"text":442},{"id":603,"depth":103,"text":604},{"id":692,"depth":103,"text":693},{"id":762,"depth":103,"text":763},"How map, filter, and reduce work in Python — lazy iterators, when they beat comprehensions, why reduce moved to functools, and the Pythonic alternatives most code should prefer.","medium","md","Python",{},"\u002Fblog\u002Fpython-map-filter-reduce-explained","\u002Fpython\u002Ffunctional\u002Fmap-filter-reduce",{"title":5,"description":821},"blog\u002Fpython-map-filter-reduce-explained","map, filter & reduce","Functional Programming","functional","2026-06-19","u1FyBGA2oho4p_BXPlgcwAj7qSLI2xgO9_EALAn3X1A",1782244092937]