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