[{"data":1,"prerenderedAt":1834},["ShallowReactive",2],{"doc-\u002Freference\u002Fpython-any-function-explained":3},{"id":4,"title":5,"body":6,"description":1827,"extension":1828,"meta":1829,"navigation":833,"path":1830,"seo":1831,"stem":1832,"__hash__":1833},"content\u002Freference\u002Fpython-any-function-explained.md","Python any() Function Explained",{"type":7,"value":8,"toc":1805},"minimark",[9,13,26,29,48,59,130,140,145,150,153,170,173,184,198,202,205,222,227,230,242,245,343,346,369,376,380,390,407,410,450,453,456,520,522,539,542,570,572,580,595,599,602,641,644,652,655,672,681,684,727,729,737,742,746,754,757,786,789,792,879,881,889,896,920,931,934,1018,1020,1028,1035,1039,1045,1061,1063,1071,1074,1080,1083,1131,1133,1150,1154,1159,1170,1173,1290,1293,1353,1359,1363,1368,1373,1376,1395,1401,1404,1423,1427,1436,1464,1466,1474,1482,1486,1492,1515,1517,1525,1532,1536,1539,1591,1594,1645,1648,1652,1656,1664,1668,1675,1679,1687,1691,1694,1716,1718,1768,1772,1801],[10,11,5],"h1",{"id":12},"python-any-function-explained",[14,15,16,20,21,25],"p",{},[17,18,19],"code",{},"any()"," is a built-in Python function that checks an iterable and tells you whether ",[22,23,24],"strong",{},"at least one item is truthy",".",[14,27,28],{},"You will usually use it with values like:",[30,31,32,36,39,42,45],"ul",{},[33,34,35],"li",{},"lists",[33,37,38],{},"tuples",[33,40,41],{},"sets",[33,43,44],{},"strings",[33,46,47],{},"generator expressions",[14,49,50,51,54,55,58],{},"It is useful when you want a quick ",[17,52,53],{},"True"," or ",[17,56,57],{},"False"," answer without writing a full loop.",[60,61,66],"pre",{"className":62,"code":63,"language":64,"meta":65,"style":65},"language-python shiki shiki-themes material-theme-lighter github-light github-dark","values = [0, '', 5]\nprint(any(values))  # True\n","python","",[17,67,68,104],{"__ignoreMap":65},[69,70,73,77,81,85,89,92,96,98,101],"span",{"class":71,"line":72},"line",1,[69,74,76],{"class":75},"su5hD","values ",[69,78,80],{"class":79},"smGrS","=",[69,82,84],{"class":83},"sP7_E"," [",[69,86,88],{"class":87},"srdBf","0",[69,90,91],{"class":83},",",[69,93,95],{"class":94},"sjJ54"," ''",[69,97,91],{"class":83},[69,99,100],{"class":87}," 5",[69,102,103],{"class":83},"]\n",[69,105,107,111,114,117,119,123,126],{"class":71,"line":106},2,[69,108,110],{"class":109},"sptTA","print",[69,112,113],{"class":83},"(",[69,115,116],{"class":109},"any",[69,118,113],{"class":83},[69,120,122],{"class":121},"slqww","values",[69,124,125],{"class":83},"))",[69,127,129],{"class":128},"sutJx","  # True\n",[14,131,132,133,136,137,139],{},"Use ",[17,134,135],{},"any(iterable)"," when you want ",[17,138,53],{}," if at least one item in the iterable is truthy.",[141,142,144],"h2",{"id":143},"what-any-does","What any() does",[14,146,147,149],{},[17,148,19],{}," checks an iterable such as a list, tuple, set, or generator.",[14,151,152],{},"It returns:",[30,154,155,160,165],{},[33,156,157,159],{},[17,158,53],{}," if at least one item is truthy",[33,161,162,164],{},[17,163,57],{}," if all items are falsy",[33,166,167,169],{},[17,168,57],{}," if the iterable is empty",[14,171,172],{},"This makes it a good choice for simple checks like:",[30,174,175,178,181],{},[33,176,177],{},"\"Does this list contain at least one real value?\"",[33,179,180],{},"\"Does any number match this condition?\"",[33,182,183],{},"\"Is there at least one non-empty string?\"",[14,185,186,187,190,191,25],{},"If you want the opposite behavior, where ",[22,188,189],{},"every"," item must be truthy, see ",[192,193,195],"a",{"href":194},"\u002Freference\u002Fpython-all-function-explained\u002F",[17,196,197],{},"all()",[141,199,201],{"id":200},"syntax","Syntax",[14,203,204],{},"The syntax is simple:",[60,206,208],{"className":62,"code":207,"language":64,"meta":65,"style":65},"any(iterable)\n",[17,209,210],{"__ignoreMap":65},[69,211,212,214,216,219],{"class":71,"line":72},[69,213,116],{"class":109},[69,215,113],{"class":83},[69,217,218],{"class":121},"iterable",[69,220,221],{"class":83},")\n",[14,223,224,225,25],{},"The argument must be an ",[22,226,218],{},[14,228,229],{},"Common inputs include:",[30,231,232,234,236,238,240],{},[33,233,35],{},[33,235,38],{},[33,237,41],{},[33,239,44],{},[33,241,47],{},[14,243,244],{},"For example:",[60,246,248],{"className":62,"code":247,"language":64,"meta":65,"style":65},"print(any([0, 0, 1]))\nprint(any((False, False)))\nprint(any({0, 2}))\nprint(any(\"hello\"))\n",[17,249,250,276,298,320],{"__ignoreMap":65},[69,251,252,254,256,258,261,263,265,268,270,273],{"class":71,"line":72},[69,253,110],{"class":109},[69,255,113],{"class":83},[69,257,116],{"class":109},[69,259,260],{"class":83},"([",[69,262,88],{"class":87},[69,264,91],{"class":83},[69,266,267],{"class":87}," 0",[69,269,91],{"class":83},[69,271,272],{"class":87}," 1",[69,274,275],{"class":83},"]))\n",[69,277,278,280,282,284,287,290,292,295],{"class":71,"line":106},[69,279,110],{"class":109},[69,281,113],{"class":83},[69,283,116],{"class":109},[69,285,286],{"class":83},"((",[69,288,57],{"class":289},"s39Yj",[69,291,91],{"class":83},[69,293,294],{"class":289}," False",[69,296,297],{"class":83},")))\n",[69,299,301,303,305,307,310,312,314,317],{"class":71,"line":300},3,[69,302,110],{"class":109},[69,304,113],{"class":83},[69,306,116],{"class":109},[69,308,309],{"class":83},"({",[69,311,88],{"class":87},[69,313,91],{"class":83},[69,315,316],{"class":87}," 2",[69,318,319],{"class":83},"}))\n",[69,321,323,325,327,329,331,334,338,340],{"class":71,"line":322},4,[69,324,110],{"class":109},[69,326,113],{"class":83},[69,328,116],{"class":109},[69,330,113],{"class":83},[69,332,333],{"class":94},"\"",[69,335,337],{"class":336},"s_sjI","hello",[69,339,333],{"class":94},[69,341,342],{"class":83},"))\n",[14,344,345],{},"Expected output:",[60,347,349],{"className":62,"code":348,"language":64,"meta":65,"style":65},"True\nFalse\nTrue\nTrue\n",[17,350,351,356,361,365],{"__ignoreMap":65},[69,352,353],{"class":71,"line":72},[69,354,355],{"class":289},"True\n",[69,357,358],{"class":71,"line":106},[69,359,360],{"class":289},"False\n",[69,362,363],{"class":71,"line":300},[69,364,355],{"class":289},[69,366,367],{"class":71,"line":322},[69,368,355],{"class":289},[14,370,371,372,25],{},"If you are not sure what an iterable is, read ",[192,373,375],{"href":374},"\u002Flearn\u002Fiterators-and-iterable-objects-explained\u002F","iterators and iterable objects explained",[141,377,379],{"id":378},"what-truthy-and-falsy-mean","What truthy and falsy mean",[14,381,382,383,54,386,389],{},"In Python, values are often treated as either ",[22,384,385],{},"truthy",[22,387,388],{},"falsy"," in conditions.",[30,391,392,400],{},[33,393,394,397,398],{},[22,395,396],{},"Truthy"," values act like ",[17,399,53],{},[33,401,402,397,405],{},[22,403,404],{},"Falsy",[17,406,57],{},[14,408,409],{},"Common falsy values include:",[30,411,412,416,420,425,430,435,440,445],{},[33,413,414],{},[17,415,57],{},[33,417,418],{},[17,419,88],{},[33,421,422],{},[17,423,424],{},"0.0",[33,426,427],{},[17,428,429],{},"''",[33,431,432],{},[17,433,434],{},"[]",[33,436,437],{},[17,438,439],{},"{}",[33,441,442],{},[17,443,444],{},"set()",[33,446,447],{},[17,448,449],{},"None",[14,451,452],{},"Most other values are truthy.",[14,454,455],{},"That means these values are truthy:",[60,457,459],{"className":62,"code":458,"language":64,"meta":65,"style":65},"print(any([1]))\nprint(any(['hello']))\nprint(any([[1, 2, 3]]))\n",[17,460,461,476,495],{"__ignoreMap":65},[69,462,463,465,467,469,471,474],{"class":71,"line":72},[69,464,110],{"class":109},[69,466,113],{"class":83},[69,468,116],{"class":109},[69,470,260],{"class":83},[69,472,473],{"class":87},"1",[69,475,275],{"class":83},[69,477,478,480,482,484,486,489,491,493],{"class":71,"line":106},[69,479,110],{"class":109},[69,481,113],{"class":83},[69,483,116],{"class":109},[69,485,260],{"class":83},[69,487,488],{"class":94},"'",[69,490,337],{"class":336},[69,492,488],{"class":94},[69,494,275],{"class":83},[69,496,497,499,501,503,506,508,510,512,514,517],{"class":71,"line":300},[69,498,110],{"class":109},[69,500,113],{"class":83},[69,502,116],{"class":109},[69,504,505],{"class":83},"([[",[69,507,473],{"class":87},[69,509,91],{"class":83},[69,511,316],{"class":87},[69,513,91],{"class":83},[69,515,516],{"class":87}," 3",[69,518,519],{"class":83},"]]))\n",[14,521,345],{},[60,523,525],{"className":62,"code":524,"language":64,"meta":65,"style":65},"True\nTrue\nTrue\n",[17,526,527,531,535],{"__ignoreMap":65},[69,528,529],{"class":71,"line":72},[69,530,355],{"class":289},[69,532,533],{"class":71,"line":106},[69,534,355],{"class":289},[69,536,537],{"class":71,"line":300},[69,538,355],{"class":289},[14,540,541],{},"And these are all falsy:",[60,543,545],{"className":62,"code":544,"language":64,"meta":65,"style":65},"print(any([0, '', None]))\n",[17,546,547],{"__ignoreMap":65},[69,548,549,551,553,555,557,559,561,563,565,568],{"class":71,"line":72},[69,550,110],{"class":109},[69,552,113],{"class":83},[69,554,116],{"class":109},[69,556,260],{"class":83},[69,558,88],{"class":87},[69,560,91],{"class":83},[69,562,95],{"class":94},[69,564,91],{"class":83},[69,566,567],{"class":289}," None",[69,569,275],{"class":83},[14,571,345],{},[60,573,574],{"className":62,"code":360,"language":64,"meta":65,"style":65},[17,575,576],{"__ignoreMap":65},[69,577,578],{"class":71,"line":72},[69,579,360],{"class":289},[14,581,582,583,587,588,25],{},"If you want to understand this more clearly, see ",[192,584,586],{"href":585},"\u002Flearn\u002Fpython-booleans-explained-true-and-false\u002F","Python booleans explained: true and false"," and ",[192,589,591,594],{"href":590},"\u002Freference\u002Fpython-bool-function-explained\u002F",[17,592,593],{},"bool()"," explained",[141,596,598],{"id":597},"basic-example","Basic example",[14,600,601],{},"Here is a simple example with one truthy value:",[60,603,605],{"className":62,"code":604,"language":64,"meta":65,"style":65},"values = [0, '', 5]\nprint(any(values))\n",[17,606,607,627],{"__ignoreMap":65},[69,608,609,611,613,615,617,619,621,623,625],{"class":71,"line":72},[69,610,76],{"class":75},[69,612,80],{"class":79},[69,614,84],{"class":83},[69,616,88],{"class":87},[69,618,91],{"class":83},[69,620,95],{"class":94},[69,622,91],{"class":83},[69,624,100],{"class":87},[69,626,103],{"class":83},[69,628,629,631,633,635,637,639],{"class":71,"line":106},[69,630,110],{"class":109},[69,632,113],{"class":83},[69,634,116],{"class":109},[69,636,113],{"class":83},[69,638,122],{"class":121},[69,640,342],{"class":83},[14,642,643],{},"Output:",[60,645,646],{"className":62,"code":355,"language":64,"meta":65,"style":65},[17,647,648],{"__ignoreMap":65},[69,649,650],{"class":71,"line":72},[69,651,355],{"class":289},[14,653,654],{},"Why?",[30,656,657,662,666],{},[33,658,659,661],{},[17,660,88],{}," is falsy",[33,663,664,661],{},[17,665,429],{},[33,667,668,671],{},[17,669,670],{},"5"," is truthy",[14,673,674,675,678,679,25],{},"Since at least one item is truthy, ",[17,676,677],{},"any(values)"," returns ",[17,680,53],{},[14,682,683],{},"Now look at a list where every item is falsy:",[60,685,687],{"className":62,"code":686,"language":64,"meta":65,"style":65},"values = [0, '', None, False]\nprint(any(values))\n",[17,688,689,713],{"__ignoreMap":65},[69,690,691,693,695,697,699,701,703,705,707,709,711],{"class":71,"line":72},[69,692,76],{"class":75},[69,694,80],{"class":79},[69,696,84],{"class":83},[69,698,88],{"class":87},[69,700,91],{"class":83},[69,702,95],{"class":94},[69,704,91],{"class":83},[69,706,567],{"class":289},[69,708,91],{"class":83},[69,710,294],{"class":289},[69,712,103],{"class":83},[69,714,715,717,719,721,723,725],{"class":71,"line":106},[69,716,110],{"class":109},[69,718,113],{"class":83},[69,720,116],{"class":109},[69,722,113],{"class":83},[69,724,122],{"class":121},[69,726,342],{"class":83},[14,728,643],{},[60,730,731],{"className":62,"code":360,"language":64,"meta":65,"style":65},[17,732,733],{"__ignoreMap":65},[69,734,735],{"class":71,"line":72},[69,736,360],{"class":289},[14,738,739,740,25],{},"Here, none of the items are truthy, so the result is ",[17,741,57],{},[141,743,745],{"id":744},"using-any-with-conditions","Using any() with conditions",[14,747,748,750,751,25],{},[17,749,19],{}," is often used with a ",[22,752,753],{},"generator expression",[14,755,756],{},"This is a common pattern:",[60,758,760],{"className":62,"code":759,"language":64,"meta":65,"style":65},"any(condition for item in items)\n",[17,761,762],{"__ignoreMap":65},[69,763,764,766,768,771,775,778,781,784],{"class":71,"line":72},[69,765,116],{"class":109},[69,767,113],{"class":83},[69,769,770],{"class":121},"condition ",[69,772,774],{"class":773},"sVHd0","for",[69,776,777],{"class":121}," item ",[69,779,780],{"class":773},"in",[69,782,783],{"class":121}," items",[69,785,221],{"class":83},[14,787,788],{},"It is useful when you want to check whether any item matches a rule.",[14,790,791],{},"For example, to check whether any number is negative:",[60,793,795],{"className":62,"code":794,"language":64,"meta":65,"style":65},"numbers = [4, 7, -2, 10]\n\nresult = any(x \u003C 0 for x in numbers)\nprint(result)\n",[17,796,797,829,835,868],{"__ignoreMap":65},[69,798,799,802,804,806,809,811,814,816,819,822,824,827],{"class":71,"line":72},[69,800,801],{"class":75},"numbers ",[69,803,80],{"class":79},[69,805,84],{"class":83},[69,807,808],{"class":87},"4",[69,810,91],{"class":83},[69,812,813],{"class":87}," 7",[69,815,91],{"class":83},[69,817,818],{"class":79}," -",[69,820,821],{"class":87},"2",[69,823,91],{"class":83},[69,825,826],{"class":87}," 10",[69,828,103],{"class":83},[69,830,831],{"class":71,"line":106},[69,832,834],{"emptyLinePlaceholder":833},true,"\n",[69,836,837,840,842,845,847,850,853,855,858,861,863,866],{"class":71,"line":300},[69,838,839],{"class":75},"result ",[69,841,80],{"class":79},[69,843,844],{"class":109}," any",[69,846,113],{"class":83},[69,848,849],{"class":121},"x ",[69,851,852],{"class":79},"\u003C",[69,854,267],{"class":87},[69,856,857],{"class":773}," for",[69,859,860],{"class":121}," x ",[69,862,780],{"class":773},[69,864,865],{"class":121}," numbers",[69,867,221],{"class":83},[69,869,870,872,874,877],{"class":71,"line":322},[69,871,110],{"class":109},[69,873,113],{"class":83},[69,875,876],{"class":121},"result",[69,878,221],{"class":83},[14,880,643],{},[60,882,883],{"className":62,"code":355,"language":64,"meta":65,"style":65},[17,884,885],{"__ignoreMap":65},[69,886,887],{"class":71,"line":72},[69,888,355],{"class":289},[14,890,891,892,895],{},"This works because ",[17,893,894],{},"x \u003C 0"," is checked for each number:",[30,897,898,906,913],{},[33,899,900,903,904],{},[17,901,902],{},"4 \u003C 0"," → ",[17,905,57],{},[33,907,908,903,911],{},[17,909,910],{},"7 \u003C 0",[17,912,57],{},[33,914,915,903,918],{},[17,916,917],{},"-2 \u003C 0",[17,919,53],{},[14,921,922,923,925,926,928,929,25],{},"As soon as Python finds one ",[17,924,53],{}," result, ",[17,927,19],{}," can return ",[17,930,53],{},[14,932,933],{},"Here is another example:",[60,935,937],{"className":62,"code":936,"language":64,"meta":65,"style":65},"words = [\"apple\", \"\", \"banana\"]\n\nhas_empty_string = any(word == \"\" for word in words)\nprint(has_empty_string)\n",[17,938,939,972,976,1007],{"__ignoreMap":65},[69,940,941,944,946,948,950,953,955,957,960,962,965,968,970],{"class":71,"line":72},[69,942,943],{"class":75},"words ",[69,945,80],{"class":79},[69,947,84],{"class":83},[69,949,333],{"class":94},[69,951,952],{"class":336},"apple",[69,954,333],{"class":94},[69,956,91],{"class":83},[69,958,959],{"class":94}," \"\"",[69,961,91],{"class":83},[69,963,964],{"class":94}," \"",[69,966,967],{"class":336},"banana",[69,969,333],{"class":94},[69,971,103],{"class":83},[69,973,974],{"class":71,"line":106},[69,975,834],{"emptyLinePlaceholder":833},[69,977,978,981,983,985,987,990,993,995,997,1000,1002,1005],{"class":71,"line":300},[69,979,980],{"class":75},"has_empty_string ",[69,982,80],{"class":79},[69,984,844],{"class":109},[69,986,113],{"class":83},[69,988,989],{"class":121},"word ",[69,991,992],{"class":79},"==",[69,994,959],{"class":94},[69,996,857],{"class":773},[69,998,999],{"class":121}," word ",[69,1001,780],{"class":773},[69,1003,1004],{"class":121}," words",[69,1006,221],{"class":83},[69,1008,1009,1011,1013,1016],{"class":71,"line":322},[69,1010,110],{"class":109},[69,1012,113],{"class":83},[69,1014,1015],{"class":121},"has_empty_string",[69,1017,221],{"class":83},[14,1019,643],{},[60,1021,1022],{"className":62,"code":355,"language":64,"meta":65,"style":65},[17,1023,1024],{"__ignoreMap":65},[69,1025,1026],{"class":71,"line":72},[69,1027,355],{"class":289},[14,1029,1030,1031,25],{},"This style is often cleaner than writing a full loop for a simple match check. For related list-checking examples, see ",[192,1032,1034],{"href":1033},"\u002Fhow-to\u002Fhow-to-check-if-a-value-exists-in-a-list-in-python\u002F","how to check if a value exists in a list in Python",[141,1036,1038],{"id":1037},"empty-iterables","Empty iterables",[14,1040,1041,1042,1044],{},"An empty iterable returns ",[17,1043,57],{},":",[60,1046,1048],{"className":62,"code":1047,"language":64,"meta":65,"style":65},"print(any([]))\n",[17,1049,1050],{"__ignoreMap":65},[69,1051,1052,1054,1056,1058],{"class":71,"line":72},[69,1053,110],{"class":109},[69,1055,113],{"class":83},[69,1057,116],{"class":109},[69,1059,1060],{"class":83},"([]))\n",[14,1062,643],{},[60,1064,1065],{"className":62,"code":360,"language":64,"meta":65,"style":65},[17,1066,1067],{"__ignoreMap":65},[69,1068,1069],{"class":71,"line":72},[69,1070,360],{"class":289},[14,1072,1073],{},"This can surprise beginners.",[14,1075,1076,1077,1079],{},"The reason is simple: ",[17,1078,19],{}," looks for at least one truthy item. In an empty iterable, there are no items at all, so there is nothing truthy to find.",[14,1081,1082],{},"The same idea applies to other empty iterables:",[60,1084,1086],{"className":62,"code":1085,"language":64,"meta":65,"style":65},"print(any(()))\nprint(any(\"\"))\nprint(any(set()))\n",[17,1087,1088,1099,1114],{"__ignoreMap":65},[69,1089,1090,1092,1094,1096],{"class":71,"line":72},[69,1091,110],{"class":109},[69,1093,113],{"class":83},[69,1095,116],{"class":109},[69,1097,1098],{"class":83},"(()))\n",[69,1100,1101,1103,1105,1107,1109,1112],{"class":71,"line":106},[69,1102,110],{"class":109},[69,1104,113],{"class":83},[69,1106,116],{"class":109},[69,1108,113],{"class":83},[69,1110,1111],{"class":94},"\"\"",[69,1113,342],{"class":83},[69,1115,1116,1118,1120,1122,1124,1128],{"class":71,"line":300},[69,1117,110],{"class":109},[69,1119,113],{"class":83},[69,1121,116],{"class":109},[69,1123,113],{"class":83},[69,1125,1127],{"class":1126},"sZMiF","set",[69,1129,1130],{"class":83},"()))\n",[14,1132,643],{},[60,1134,1136],{"className":62,"code":1135,"language":64,"meta":65,"style":65},"False\nFalse\nFalse\n",[17,1137,1138,1142,1146],{"__ignoreMap":65},[69,1139,1140],{"class":71,"line":72},[69,1141,360],{"class":289},[69,1143,1144],{"class":71,"line":106},[69,1145,360],{"class":289},[69,1147,1148],{"class":71,"line":300},[69,1149,360],{"class":289},[141,1151,1153],{"id":1152},"when-to-use-any","When to use any()",[14,1155,132,1156,1158],{},[17,1157,19],{}," when you want to:",[30,1160,1161,1164,1167],{},[33,1162,1163],{},"check whether a list contains at least one matching value",[33,1165,1166],{},"simplify loops that only need one match",[33,1168,1169],{},"make condition checks shorter and easier to read",[14,1171,1172],{},"For example, instead of writing this:",[60,1174,1176],{"className":62,"code":1175,"language":64,"meta":65,"style":65},"numbers = [1, 3, 5, 8]\n\nfound_even = False\n\nfor number in numbers:\n    if number % 2 == 0:\n        found_even = True\n        break\n\nprint(found_even)\n",[17,1177,1178,1203,1207,1217,1221,1236,1256,1267,1273,1278],{"__ignoreMap":65},[69,1179,1180,1182,1184,1186,1188,1190,1192,1194,1196,1198,1201],{"class":71,"line":72},[69,1181,801],{"class":75},[69,1183,80],{"class":79},[69,1185,84],{"class":83},[69,1187,473],{"class":87},[69,1189,91],{"class":83},[69,1191,516],{"class":87},[69,1193,91],{"class":83},[69,1195,100],{"class":87},[69,1197,91],{"class":83},[69,1199,1200],{"class":87}," 8",[69,1202,103],{"class":83},[69,1204,1205],{"class":71,"line":106},[69,1206,834],{"emptyLinePlaceholder":833},[69,1208,1209,1212,1214],{"class":71,"line":300},[69,1210,1211],{"class":75},"found_even ",[69,1213,80],{"class":79},[69,1215,1216],{"class":289}," False\n",[69,1218,1219],{"class":71,"line":322},[69,1220,834],{"emptyLinePlaceholder":833},[69,1222,1224,1226,1229,1231,1233],{"class":71,"line":1223},5,[69,1225,774],{"class":773},[69,1227,1228],{"class":75}," number ",[69,1230,780],{"class":773},[69,1232,865],{"class":75},[69,1234,1235],{"class":83},":\n",[69,1237,1239,1242,1244,1247,1249,1252,1254],{"class":71,"line":1238},6,[69,1240,1241],{"class":773},"    if",[69,1243,1228],{"class":75},[69,1245,1246],{"class":79},"%",[69,1248,316],{"class":87},[69,1250,1251],{"class":79}," ==",[69,1253,267],{"class":87},[69,1255,1235],{"class":83},[69,1257,1259,1262,1264],{"class":71,"line":1258},7,[69,1260,1261],{"class":75},"        found_even ",[69,1263,80],{"class":79},[69,1265,1266],{"class":289}," True\n",[69,1268,1270],{"class":71,"line":1269},8,[69,1271,1272],{"class":773},"        break\n",[69,1274,1276],{"class":71,"line":1275},9,[69,1277,834],{"emptyLinePlaceholder":833},[69,1279,1281,1283,1285,1288],{"class":71,"line":1280},10,[69,1282,110],{"class":109},[69,1284,113],{"class":83},[69,1286,1287],{"class":121},"found_even",[69,1289,221],{"class":83},[14,1291,1292],{},"You can write:",[60,1294,1296],{"className":62,"code":1295,"language":64,"meta":65,"style":65},"numbers = [1, 3, 5, 8]\nprint(any(number % 2 == 0 for number in numbers))\n",[17,1297,1298,1322],{"__ignoreMap":65},[69,1299,1300,1302,1304,1306,1308,1310,1312,1314,1316,1318,1320],{"class":71,"line":72},[69,1301,801],{"class":75},[69,1303,80],{"class":79},[69,1305,84],{"class":83},[69,1307,473],{"class":87},[69,1309,91],{"class":83},[69,1311,516],{"class":87},[69,1313,91],{"class":83},[69,1315,100],{"class":87},[69,1317,91],{"class":83},[69,1319,1200],{"class":87},[69,1321,103],{"class":83},[69,1323,1324,1326,1328,1330,1332,1335,1337,1339,1341,1343,1345,1347,1349,1351],{"class":71,"line":106},[69,1325,110],{"class":109},[69,1327,113],{"class":83},[69,1329,116],{"class":109},[69,1331,113],{"class":83},[69,1333,1334],{"class":121},"number ",[69,1336,1246],{"class":79},[69,1338,316],{"class":87},[69,1340,1251],{"class":79},[69,1342,267],{"class":87},[69,1344,857],{"class":773},[69,1346,1228],{"class":121},[69,1348,780],{"class":773},[69,1350,865],{"class":121},[69,1352,342],{"class":83},[14,1354,1355,1356,1358],{},"Both work, but ",[17,1357,19],{}," is shorter and easier to read when you only need a yes-or-no answer.",[141,1360,1362],{"id":1361},"common-beginner-mistakes","Common beginner mistakes",[14,1364,1365,1366,25],{},"Here are some common mistakes with ",[17,1367,19],{},[1369,1370,1372],"h3",{"id":1371},"passing-a-single-number-instead-of-an-iterable","Passing a single number instead of an iterable",[14,1374,1375],{},"This is wrong:",[60,1377,1379],{"className":62,"code":1378,"language":64,"meta":65,"style":65},"print(any(5))\n",[17,1380,1381],{"__ignoreMap":65},[69,1382,1383,1385,1387,1389,1391,1393],{"class":71,"line":72},[69,1384,110],{"class":109},[69,1386,113],{"class":83},[69,1388,116],{"class":109},[69,1390,113],{"class":83},[69,1392,670],{"class":87},[69,1394,342],{"class":83},[14,1396,1397,1398,1400],{},"It causes an error because ",[17,1399,670],{}," is not iterable.",[14,1402,1403],{},"Use an iterable instead:",[60,1405,1407],{"className":62,"code":1406,"language":64,"meta":65,"style":65},"print(any([5]))\n",[17,1408,1409],{"__ignoreMap":65},[69,1410,1411,1413,1415,1417,1419,1421],{"class":71,"line":72},[69,1412,110],{"class":109},[69,1414,113],{"class":83},[69,1416,116],{"class":109},[69,1418,260],{"class":83},[69,1420,670],{"class":87},[69,1422,275],{"class":83},[1369,1424,1426],{"id":1425},"thinking-any-checks-whether-all-values-are-true","Thinking any() checks whether all values are True",[14,1428,1429,1431,1432,1435],{},[17,1430,19],{}," needs only ",[22,1433,1434],{},"one"," truthy item.",[60,1437,1439],{"className":62,"code":1438,"language":64,"meta":65,"style":65},"print(any([False, False, True]))\n",[17,1440,1441],{"__ignoreMap":65},[69,1442,1443,1445,1447,1449,1451,1453,1455,1457,1459,1462],{"class":71,"line":72},[69,1444,110],{"class":109},[69,1446,113],{"class":83},[69,1448,116],{"class":109},[69,1450,260],{"class":83},[69,1452,57],{"class":289},[69,1454,91],{"class":83},[69,1456,294],{"class":289},[69,1458,91],{"class":83},[69,1460,1461],{"class":289}," True",[69,1463,275],{"class":83},[14,1465,643],{},[60,1467,1468],{"className":62,"code":355,"language":64,"meta":65,"style":65},[17,1469,1470],{"__ignoreMap":65},[69,1471,1472],{"class":71,"line":72},[69,1473,355],{"class":289},[14,1475,1476,1477,1481],{},"If you need every item to be truthy, use ",[192,1478,1479],{"href":194},[17,1480,197],{}," instead.",[1369,1483,1485],{"id":1484},"forgetting-that-non-empty-strings-are-truthy","Forgetting that non-empty strings are truthy",[14,1487,1488,1489,25],{},"A non-empty string is truthy, even if it is not ",[17,1490,1491],{},"\"True\"",[60,1493,1495],{"className":62,"code":1494,"language":64,"meta":65,"style":65},"print(any([\"False\"]))\n",[17,1496,1497],{"__ignoreMap":65},[69,1498,1499,1501,1503,1505,1507,1509,1511,1513],{"class":71,"line":72},[69,1500,110],{"class":109},[69,1502,113],{"class":83},[69,1504,116],{"class":109},[69,1506,260],{"class":83},[69,1508,333],{"class":94},[69,1510,57],{"class":336},[69,1512,333],{"class":94},[69,1514,275],{"class":83},[14,1516,643],{},[60,1518,1519],{"className":62,"code":355,"language":64,"meta":65,"style":65},[17,1520,1521],{"__ignoreMap":65},[69,1522,1523],{"class":71,"line":72},[69,1524,355],{"class":289},[14,1526,1527,1528,1531],{},"That is because ",[17,1529,1530],{},"\"False\""," is a non-empty string.",[1369,1533,1535],{"id":1534},"using-a-list-comprehension-when-a-generator-expression-is-enough","Using a list comprehension when a generator expression is enough",[14,1537,1538],{},"This works:",[60,1540,1542],{"className":62,"code":1541,"language":64,"meta":65,"style":65},"numbers = [1, 2, 3]\nprint(any([x > 2 for x in numbers]))\n",[17,1543,1544,1564],{"__ignoreMap":65},[69,1545,1546,1548,1550,1552,1554,1556,1558,1560,1562],{"class":71,"line":72},[69,1547,801],{"class":75},[69,1549,80],{"class":79},[69,1551,84],{"class":83},[69,1553,473],{"class":87},[69,1555,91],{"class":83},[69,1557,316],{"class":87},[69,1559,91],{"class":83},[69,1561,516],{"class":87},[69,1563,103],{"class":83},[69,1565,1566,1568,1570,1572,1574,1576,1579,1581,1583,1585,1587,1589],{"class":71,"line":106},[69,1567,110],{"class":109},[69,1569,113],{"class":83},[69,1571,116],{"class":109},[69,1573,260],{"class":83},[69,1575,849],{"class":121},[69,1577,1578],{"class":79},">",[69,1580,316],{"class":87},[69,1582,857],{"class":773},[69,1584,860],{"class":121},[69,1586,780],{"class":773},[69,1588,865],{"class":121},[69,1590,275],{"class":83},[14,1592,1593],{},"But this is usually better:",[60,1595,1597],{"className":62,"code":1596,"language":64,"meta":65,"style":65},"numbers = [1, 2, 3]\nprint(any(x > 2 for x in numbers))\n",[17,1598,1599,1619],{"__ignoreMap":65},[69,1600,1601,1603,1605,1607,1609,1611,1613,1615,1617],{"class":71,"line":72},[69,1602,801],{"class":75},[69,1604,80],{"class":79},[69,1606,84],{"class":83},[69,1608,473],{"class":87},[69,1610,91],{"class":83},[69,1612,316],{"class":87},[69,1614,91],{"class":83},[69,1616,516],{"class":87},[69,1618,103],{"class":83},[69,1620,1621,1623,1625,1627,1629,1631,1633,1635,1637,1639,1641,1643],{"class":71,"line":106},[69,1622,110],{"class":109},[69,1624,113],{"class":83},[69,1626,116],{"class":109},[69,1628,113],{"class":83},[69,1630,849],{"class":121},[69,1632,1578],{"class":79},[69,1634,316],{"class":87},[69,1636,857],{"class":773},[69,1638,860],{"class":121},[69,1640,780],{"class":773},[69,1642,865],{"class":121},[69,1644,342],{"class":83},[14,1646,1647],{},"The second version avoids creating an unnecessary list.",[141,1649,1651],{"id":1650},"faq","FAQ",[1369,1653,1655],{"id":1654},"what-does-any-return-in-python","What does any() return in Python?",[14,1657,1658,1659,1661,1662,25],{},"It returns ",[17,1660,53],{}," if at least one item in the iterable is truthy. Otherwise it returns ",[17,1663,57],{},[1369,1665,1667],{"id":1666},"what-happens-if-the-iterable-is-empty","What happens if the iterable is empty?",[14,1669,1670,678,1672,1674],{},[17,1671,19],{},[17,1673,57],{}," for an empty iterable.",[1369,1676,1678],{"id":1677},"what-is-the-difference-between-any-and-all","What is the difference between any() and all()?",[14,1680,1681,1683,1684,1686],{},[17,1682,19],{}," needs one truthy item. ",[17,1685,197],{}," needs every item to be truthy.",[1369,1688,1690],{"id":1689},"can-i-use-any-with-a-condition","Can I use any() with a condition?",[14,1692,1693],{},"Yes. A common pattern is:",[60,1695,1696],{"className":62,"code":759,"language":64,"meta":65,"style":65},[17,1697,1698],{"__ignoreMap":65},[69,1699,1700,1702,1704,1706,1708,1710,1712,1714],{"class":71,"line":72},[69,1701,116],{"class":109},[69,1703,113],{"class":83},[69,1705,770],{"class":121},[69,1707,774],{"class":773},[69,1709,777],{"class":121},[69,1711,780],{"class":773},[69,1713,783],{"class":121},[69,1715,221],{"class":83},[14,1717,244],{},[60,1719,1720],{"className":62,"code":1596,"language":64,"meta":65,"style":65},[17,1721,1722,1742],{"__ignoreMap":65},[69,1723,1724,1726,1728,1730,1732,1734,1736,1738,1740],{"class":71,"line":72},[69,1725,801],{"class":75},[69,1727,80],{"class":79},[69,1729,84],{"class":83},[69,1731,473],{"class":87},[69,1733,91],{"class":83},[69,1735,316],{"class":87},[69,1737,91],{"class":83},[69,1739,516],{"class":87},[69,1741,103],{"class":83},[69,1743,1744,1746,1748,1750,1752,1754,1756,1758,1760,1762,1764,1766],{"class":71,"line":106},[69,1745,110],{"class":109},[69,1747,113],{"class":83},[69,1749,116],{"class":109},[69,1751,113],{"class":83},[69,1753,849],{"class":121},[69,1755,1578],{"class":79},[69,1757,316],{"class":87},[69,1759,857],{"class":773},[69,1761,860],{"class":121},[69,1763,780],{"class":773},[69,1765,865],{"class":121},[69,1767,342],{"class":83},[141,1769,1771],{"id":1770},"see-also","See also",[30,1773,1774,1781,1785,1790,1795],{},[33,1775,1776],{},[192,1777,1778,1780],{"href":194},[17,1779,197],{}," function explained",[33,1782,1783],{},[192,1784,586],{"href":585},[33,1786,1787],{},[192,1788,1789],{"href":374},"Iterators and iterable objects explained",[33,1791,1792],{},[192,1793,1794],{"href":1033},"How to check if a value exists in a list in Python",[33,1796,1797],{},[192,1798,1799,1780],{"href":590},[17,1800,593],{},[1802,1803,1804],"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 .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--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 pre.shiki code .sjJ54, html code.shiki .sjJ54{--shiki-light:#39ADB5;--shiki-default:#032F62;--shiki-dark:#9ECBFF}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 .sutJx, html code.shiki .sutJx{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#6A737D;--shiki-default-font-style:inherit;--shiki-dark:#6A737D;--shiki-dark-font-style:inherit}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 .s39Yj, html code.shiki .s39Yj{--shiki-light:#39ADB5;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .s_sjI, html code.shiki .s_sjI{--shiki-light:#91B859;--shiki-default:#032F62;--shiki-dark:#9ECBFF}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 .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":65,"searchDepth":106,"depth":106,"links":1806},[1807,1808,1809,1810,1811,1812,1813,1814,1820,1826],{"id":143,"depth":106,"text":144},{"id":200,"depth":106,"text":201},{"id":378,"depth":106,"text":379},{"id":597,"depth":106,"text":598},{"id":744,"depth":106,"text":745},{"id":1037,"depth":106,"text":1038},{"id":1152,"depth":106,"text":1153},{"id":1361,"depth":106,"text":1362,"children":1815},[1816,1817,1818,1819],{"id":1371,"depth":300,"text":1372},{"id":1425,"depth":300,"text":1426},{"id":1484,"depth":300,"text":1485},{"id":1534,"depth":300,"text":1535},{"id":1650,"depth":106,"text":1651,"children":1821},[1822,1823,1824,1825],{"id":1654,"depth":300,"text":1655},{"id":1666,"depth":300,"text":1667},{"id":1677,"depth":300,"text":1678},{"id":1689,"depth":300,"text":1690},{"id":1770,"depth":106,"text":1771},"Master python any function explained in our comprehensive Python beginner guide.","md",{},"\u002Freference\u002Fpython-any-function-explained",{"title":5,"description":1827},"reference\u002Fpython-any-function-explained","uL7-T2Lk2YfmN9VEO8KlebxCO6XTQT36wCuJPf1m2Jw",1777585487776]