[{"data":1,"prerenderedAt":1629},["ShallowReactive",2],{"doc-\u002Ferrors\u002Fstopiteration-exception-in-python-explained":3},{"id":4,"title":5,"body":6,"description":1622,"extension":1623,"meta":1624,"navigation":138,"path":1625,"seo":1626,"stem":1627,"__hash__":1628},"content\u002Ferrors\u002Fstopiteration-exception-in-python-explained.md","StopIteration Exception in Python Explained",{"type":7,"value":8,"toc":1592},"minimark",[9,13,26,36,39,66,76,81,90,216,219,239,255,259,264,267,278,291,299,303,309,336,339,416,419,423,426,495,497,534,537,565,569,578,581,634,636,649,655,664,670,674,684,690,791,793,811,817,823,905,907,925,934,940,947,956,1006,1008,1020,1023,1030,1034,1040,1043,1054,1064,1068,1102,1105,1109,1133,1136,1155,1158,1165,1169,1175,1204,1207,1283,1286,1316,1319,1338,1341,1345,1348,1373,1377,1380,1481,1483,1508,1511,1515,1519,1522,1526,1529,1533,1541,1545,1551,1555,1588],[10,11,5],"h1",{"id":12},"stopiteration-exception-in-python-explained",[14,15,16,20,21,25],"p",{},[17,18,19],"code",{},"StopIteration"," happens when an ",[22,23,24],"strong",{},"iterator"," has no more values to return.",[14,27,28,29,32,33,35],{},"This does ",[22,30,31],{},"not always mean something is broken",". In Python, ",[17,34,19],{}," is a normal signal used to say, “there are no more items.”",[14,37,38],{},"Beginners usually see it when:",[40,41,42,50,57,60],"ul",{},[43,44,45,46,49],"li",{},"calling ",[17,47,48],{},"next()"," too many times",[43,51,52,53,56],{},"using a ",[17,54,55],{},"while True"," loop with an iterator",[43,58,59],{},"reusing an iterator that is already finished",[43,61,62,63,65],{},"raising ",[17,64,19],{}," manually inside a generator",[14,67,68,69,72,73,75],{},"If you use a ",[17,70,71],{},"for"," loop, Python usually handles this for you automatically. The exception becomes visible when you call ",[17,74,48],{}," yourself and do not handle the end of the iterator.",[77,78,80],"h2",{"id":79},"quick-fix","Quick fix",[14,82,83,84,86,87,89],{},"If you are calling ",[17,85,48],{}," manually, catch ",[17,88,19],{},":",[91,92,97],"pre",{"className":93,"code":94,"language":95,"meta":96,"style":96},"language-python shiki shiki-themes material-theme-lighter github-light github-dark","numbers = iter([1, 2])\n\ntry:\n    while True:\n        print(next(numbers))\nexcept StopIteration:\n    print(\"No more items\")\n","python","",[17,98,99,133,140,150,162,183,195],{"__ignoreMap":96},[100,101,104,108,112,116,120,124,127,130],"span",{"class":102,"line":103},"line",1,[100,105,107],{"class":106},"su5hD","numbers ",[100,109,111],{"class":110},"smGrS","=",[100,113,115],{"class":114},"sptTA"," iter",[100,117,119],{"class":118},"sP7_E","([",[100,121,123],{"class":122},"srdBf","1",[100,125,126],{"class":118},",",[100,128,129],{"class":122}," 2",[100,131,132],{"class":118},"])\n",[100,134,136],{"class":102,"line":135},2,[100,137,139],{"emptyLinePlaceholder":138},true,"\n",[100,141,143,147],{"class":102,"line":142},3,[100,144,146],{"class":145},"sVHd0","try",[100,148,149],{"class":118},":\n",[100,151,153,156,160],{"class":102,"line":152},4,[100,154,155],{"class":145},"    while",[100,157,159],{"class":158},"s39Yj"," True",[100,161,149],{"class":118},[100,163,165,168,171,174,176,180],{"class":102,"line":164},5,[100,166,167],{"class":114},"        print",[100,169,170],{"class":118},"(",[100,172,173],{"class":114},"next",[100,175,170],{"class":118},[100,177,179],{"class":178},"slqww","numbers",[100,181,182],{"class":118},"))\n",[100,184,186,189,193],{"class":102,"line":185},6,[100,187,188],{"class":145},"except",[100,190,192],{"class":191},"sZMiF"," StopIteration",[100,194,149],{"class":118},[100,196,198,201,203,207,211,213],{"class":102,"line":197},7,[100,199,200],{"class":114},"    print",[100,202,170],{"class":118},[100,204,206],{"class":205},"sjJ54","\"",[100,208,210],{"class":209},"s_sjI","No more items",[100,212,206],{"class":205},[100,214,215],{"class":118},")\n",[14,217,218],{},"Output:",[91,220,222],{"className":93,"code":221,"language":95,"meta":96,"style":96},"1\n2\nNo more items\n",[17,223,224,229,234],{"__ignoreMap":96},[100,225,226],{"class":102,"line":103},[100,227,228],{"class":122},"1\n",[100,230,231],{"class":102,"line":135},[100,232,233],{"class":122},"2\n",[100,235,236],{"class":102,"line":142},[100,237,238],{"class":106},"No more items\n",[14,240,241,242,245,246,248,249,251,252,254],{},"Use ",[17,243,244],{},"try-except"," when you call ",[17,247,48],{}," yourself. In a ",[17,250,71],{}," loop, Python handles ",[17,253,19],{}," automatically.",[77,256,258],{"id":257},"what-stopiteration-means","What StopIteration means",[14,260,261,263],{},[17,262,19],{}," is raised when an iterator has no more values to give.",[14,265,266],{},"Important points:",[40,268,269,272,275],{},[43,270,271],{},"It is part of normal Python iteration",[43,273,274],{},"It does not always mean there is a bug",[43,276,277],{},"It is how Python knows an iterator is finished",[14,279,280,281,284,285,287,288,290],{},"For example, a list can be turned into an iterator with ",[17,282,283],{},"iter()",". Each call to ",[17,286,48],{}," gets one item. When there are no items left, Python raises ",[17,289,19],{},".",[14,292,293,294,290],{},"If iterators are new to you, see ",[295,296,298],"a",{"href":297},"\u002Flearn\u002Fiterators-and-iterable-objects-explained","iterators and iterable objects explained",[77,300,302],{"id":301},"when-beginners-usually-see-this-error","When beginners usually see this error",[14,304,305,306,308],{},"You will usually run into ",[17,307,19],{}," in these situations:",[40,310,311,318,323,331],{},[43,312,313],{},[22,314,315,316,49],{},"Calling ",[17,317,48],{},[43,319,320],{},[22,321,322],{},"Reading past the end of a custom iterator",[43,324,325],{},[22,326,327,328,330],{},"Raising ",[17,329,19],{}," directly inside a generator",[43,332,333],{},[22,334,335],{},"Using iterator code without checking when it is finished",[14,337,338],{},"A very common example is this:",[91,340,342],{"className":93,"code":341,"language":95,"meta":96,"style":96},"numbers = iter([10, 20])\n\nprint(next(numbers))\nprint(next(numbers))\nprint(next(numbers))  # No more values left\n",[17,343,344,364,368,383,397],{"__ignoreMap":96},[100,345,346,348,350,352,354,357,359,362],{"class":102,"line":103},[100,347,107],{"class":106},[100,349,111],{"class":110},[100,351,115],{"class":114},[100,353,119],{"class":118},[100,355,356],{"class":122},"10",[100,358,126],{"class":118},[100,360,361],{"class":122}," 20",[100,363,132],{"class":118},[100,365,366],{"class":102,"line":135},[100,367,139],{"emptyLinePlaceholder":138},[100,369,370,373,375,377,379,381],{"class":102,"line":142},[100,371,372],{"class":114},"print",[100,374,170],{"class":118},[100,376,173],{"class":114},[100,378,170],{"class":118},[100,380,179],{"class":178},[100,382,182],{"class":118},[100,384,385,387,389,391,393,395],{"class":102,"line":152},[100,386,372],{"class":114},[100,388,170],{"class":118},[100,390,173],{"class":114},[100,392,170],{"class":118},[100,394,179],{"class":178},[100,396,182],{"class":118},[100,398,399,401,403,405,407,409,412],{"class":102,"line":164},[100,400,372],{"class":114},[100,402,170],{"class":118},[100,404,173],{"class":114},[100,406,170],{"class":118},[100,408,179],{"class":178},[100,410,411],{"class":118},"))",[100,413,415],{"class":414},"sutJx","  # No more values left\n",[14,417,418],{},"The third call fails because the iterator is already exhausted.",[77,420,422],{"id":421},"example-that-causes-stopiteration","Example that causes StopIteration",[14,424,425],{},"Here is a short example that raises the exception:",[91,427,429],{"className":93,"code":428,"language":95,"meta":96,"style":96},"numbers = iter([1, 2])\n\nprint(next(numbers))\nprint(next(numbers))\nprint(next(numbers))\n",[17,430,431,449,453,467,481],{"__ignoreMap":96},[100,432,433,435,437,439,441,443,445,447],{"class":102,"line":103},[100,434,107],{"class":106},[100,436,111],{"class":110},[100,438,115],{"class":114},[100,440,119],{"class":118},[100,442,123],{"class":122},[100,444,126],{"class":118},[100,446,129],{"class":122},[100,448,132],{"class":118},[100,450,451],{"class":102,"line":135},[100,452,139],{"emptyLinePlaceholder":138},[100,454,455,457,459,461,463,465],{"class":102,"line":142},[100,456,372],{"class":114},[100,458,170],{"class":118},[100,460,173],{"class":114},[100,462,170],{"class":118},[100,464,179],{"class":178},[100,466,182],{"class":118},[100,468,469,471,473,475,477,479],{"class":102,"line":152},[100,470,372],{"class":114},[100,472,170],{"class":118},[100,474,173],{"class":114},[100,476,170],{"class":118},[100,478,179],{"class":178},[100,480,182],{"class":118},[100,482,483,485,487,489,491,493],{"class":102,"line":164},[100,484,372],{"class":114},[100,486,170],{"class":118},[100,488,173],{"class":114},[100,490,170],{"class":118},[100,492,179],{"class":178},[100,494,182],{"class":118},[14,496,218],{},[91,498,500],{"className":93,"code":499,"language":95,"meta":96,"style":96},"1\n2\nTraceback (most recent call last):\n  ...\nStopIteration\n",[17,501,502,506,510,523,529],{"__ignoreMap":96},[100,503,504],{"class":102,"line":103},[100,505,228],{"class":122},[100,507,508],{"class":102,"line":135},[100,509,233],{"class":122},[100,511,512,515,517,520],{"class":102,"line":142},[100,513,514],{"class":178},"Traceback ",[100,516,170],{"class":118},[100,518,519],{"class":178},"most recent call last",[100,521,522],{"class":118},"):\n",[100,524,525],{"class":102,"line":152},[100,526,528],{"class":527},"s_hVV","  ...\n",[100,530,531],{"class":102,"line":164},[100,532,533],{"class":191},"StopIteration\n",[14,535,536],{},"What happens here:",[40,538,539,545,554,560],{},[43,540,541,544],{},[17,542,543],{},"iter([1, 2])"," creates an iterator",[43,546,547,548,551,552],{},"the first ",[17,549,550],{},"next(numbers)"," returns ",[17,553,123],{},[43,555,556,557],{},"the second returns ",[17,558,559],{},"2",[43,561,562,563],{},"the third has nothing left to return, so Python raises ",[17,564,19],{},[77,566,568],{"id":567},"why-for-loops-usually-do-not-crash","Why for loops usually do not crash",[14,570,571,572,574,575,577],{},"A ",[17,573,71],{}," loop uses ",[17,576,48],{}," behind the scenes.",[14,579,580],{},"For example:",[91,582,584],{"className":93,"code":583,"language":95,"meta":96,"style":96},"numbers = [1, 2]\n\nfor number in numbers:\n    print(number)\n",[17,585,586,604,608,623],{"__ignoreMap":96},[100,587,588,590,592,595,597,599,601],{"class":102,"line":103},[100,589,107],{"class":106},[100,591,111],{"class":110},[100,593,594],{"class":118}," [",[100,596,123],{"class":122},[100,598,126],{"class":118},[100,600,129],{"class":122},[100,602,603],{"class":118},"]\n",[100,605,606],{"class":102,"line":135},[100,607,139],{"emptyLinePlaceholder":138},[100,609,610,612,615,618,621],{"class":102,"line":142},[100,611,71],{"class":145},[100,613,614],{"class":106}," number ",[100,616,617],{"class":145},"in",[100,619,620],{"class":106}," numbers",[100,622,149],{"class":118},[100,624,625,627,629,632],{"class":102,"line":152},[100,626,200],{"class":114},[100,628,170],{"class":118},[100,630,631],{"class":178},"number",[100,633,215],{"class":118},[14,635,218],{},[91,637,639],{"className":93,"code":638,"language":95,"meta":96,"style":96},"1\n2\n",[17,640,641,645],{"__ignoreMap":96},[100,642,643],{"class":102,"line":103},[100,644,228],{"class":122},[100,646,647],{"class":102,"line":135},[100,648,233],{"class":122},[14,650,651,652,654],{},"This loop stops cleanly because Python catches ",[17,653,19],{}," internally.",[14,656,657,658,660,661,663],{},"That is why normal ",[17,659,71],{}," loops do not usually show this error. The loop keeps calling ",[17,662,48],{}," until the iterator is finished, then it stops.",[14,665,666,667,669],{},"This is one reason ",[17,668,71],{}," loops are often safer than manual iterator code.",[77,671,673],{"id":672},"how-to-fix-it-when-using-next","How to fix it when using next()",[675,676,678,679,681,682],"h3",{"id":677},"wrap-next-in-try-except","Wrap ",[17,680,48],{}," in ",[17,683,244],{},[14,685,686,687,689],{},"If you want to keep calling ",[17,688,48],{}," yourself, catch the exception:",[91,691,693],{"className":93,"code":692,"language":95,"meta":96,"style":96},"numbers = iter([1, 2])\n\nwhile True:\n    try:\n        value = next(numbers)\n        print(value)\n    except StopIteration:\n        print(\"Finished\")\n        break\n",[17,694,695,713,717,726,733,749,760,769,785],{"__ignoreMap":96},[100,696,697,699,701,703,705,707,709,711],{"class":102,"line":103},[100,698,107],{"class":106},[100,700,111],{"class":110},[100,702,115],{"class":114},[100,704,119],{"class":118},[100,706,123],{"class":122},[100,708,126],{"class":118},[100,710,129],{"class":122},[100,712,132],{"class":118},[100,714,715],{"class":102,"line":135},[100,716,139],{"emptyLinePlaceholder":138},[100,718,719,722,724],{"class":102,"line":142},[100,720,721],{"class":145},"while",[100,723,159],{"class":158},[100,725,149],{"class":118},[100,727,728,731],{"class":102,"line":152},[100,729,730],{"class":145},"    try",[100,732,149],{"class":118},[100,734,735,738,740,743,745,747],{"class":102,"line":164},[100,736,737],{"class":106},"        value ",[100,739,111],{"class":110},[100,741,742],{"class":114}," next",[100,744,170],{"class":118},[100,746,179],{"class":178},[100,748,215],{"class":118},[100,750,751,753,755,758],{"class":102,"line":185},[100,752,167],{"class":114},[100,754,170],{"class":118},[100,756,757],{"class":178},"value",[100,759,215],{"class":118},[100,761,762,765,767],{"class":102,"line":197},[100,763,764],{"class":145},"    except",[100,766,192],{"class":191},[100,768,149],{"class":118},[100,770,772,774,776,778,781,783],{"class":102,"line":771},8,[100,773,167],{"class":114},[100,775,170],{"class":118},[100,777,206],{"class":205},[100,779,780],{"class":209},"Finished",[100,782,206],{"class":205},[100,784,215],{"class":118},[100,786,788],{"class":102,"line":787},9,[100,789,790],{"class":145},"        break\n",[14,792,218],{},[91,794,796],{"className":93,"code":795,"language":95,"meta":96,"style":96},"1\n2\nFinished\n",[17,797,798,802,806],{"__ignoreMap":96},[100,799,800],{"class":102,"line":103},[100,801,228],{"class":122},[100,803,804],{"class":102,"line":135},[100,805,233],{"class":122},[100,807,808],{"class":102,"line":142},[100,809,810],{"class":106},"Finished\n",[675,812,814,815],{"id":813},"use-a-default-value-with-next","Use a default value with ",[17,816,48],{},[14,818,819,820,822],{},"You can avoid the exception by giving ",[17,821,48],{}," a default value:",[91,824,826],{"className":93,"code":825,"language":95,"meta":96,"style":96},"numbers = iter([1, 2])\n\nprint(next(numbers, None))\nprint(next(numbers, None))\nprint(next(numbers, None))\n",[17,827,828,846,850,869,887],{"__ignoreMap":96},[100,829,830,832,834,836,838,840,842,844],{"class":102,"line":103},[100,831,107],{"class":106},[100,833,111],{"class":110},[100,835,115],{"class":114},[100,837,119],{"class":118},[100,839,123],{"class":122},[100,841,126],{"class":118},[100,843,129],{"class":122},[100,845,132],{"class":118},[100,847,848],{"class":102,"line":135},[100,849,139],{"emptyLinePlaceholder":138},[100,851,852,854,856,858,860,862,864,867],{"class":102,"line":142},[100,853,372],{"class":114},[100,855,170],{"class":118},[100,857,173],{"class":114},[100,859,170],{"class":118},[100,861,179],{"class":178},[100,863,126],{"class":118},[100,865,866],{"class":158}," None",[100,868,182],{"class":118},[100,870,871,873,875,877,879,881,883,885],{"class":102,"line":152},[100,872,372],{"class":114},[100,874,170],{"class":118},[100,876,173],{"class":114},[100,878,170],{"class":118},[100,880,179],{"class":178},[100,882,126],{"class":118},[100,884,866],{"class":158},[100,886,182],{"class":118},[100,888,889,891,893,895,897,899,901,903],{"class":102,"line":164},[100,890,372],{"class":114},[100,892,170],{"class":118},[100,894,173],{"class":114},[100,896,170],{"class":118},[100,898,179],{"class":178},[100,900,126],{"class":118},[100,902,866],{"class":158},[100,904,182],{"class":118},[14,906,218],{},[91,908,910],{"className":93,"code":909,"language":95,"meta":96,"style":96},"1\n2\nNone\n",[17,911,912,916,920],{"__ignoreMap":96},[100,913,914],{"class":102,"line":103},[100,915,228],{"class":122},[100,917,918],{"class":102,"line":135},[100,919,233],{"class":122},[100,921,922],{"class":102,"line":142},[100,923,924],{"class":158},"None\n",[14,926,927,928,931,932,290],{},"The third call returns ",[17,929,930],{},"None"," instead of raising ",[17,933,19],{},[14,935,936,937,939],{},"This is often the simplest fix when ",[17,938,930],{}," is an acceptable “no more data” value.",[675,941,943,944,946],{"id":942},"stop-calling-next-when-the-iterator-is-finished","Stop calling ",[17,945,48],{}," when the iterator is finished",[14,948,949,950,952,953,955],{},"In many cases, the best fix is to use a ",[17,951,71],{}," loop instead of manual ",[17,954,48],{}," calls:",[91,957,959],{"className":93,"code":958,"language":95,"meta":96,"style":96},"numbers = iter([1, 2])\n\nfor value in numbers:\n    print(value)\n",[17,960,961,979,983,996],{"__ignoreMap":96},[100,962,963,965,967,969,971,973,975,977],{"class":102,"line":103},[100,964,107],{"class":106},[100,966,111],{"class":110},[100,968,115],{"class":114},[100,970,119],{"class":118},[100,972,123],{"class":122},[100,974,126],{"class":118},[100,976,129],{"class":122},[100,978,132],{"class":118},[100,980,981],{"class":102,"line":135},[100,982,139],{"emptyLinePlaceholder":138},[100,984,985,987,990,992,994],{"class":102,"line":142},[100,986,71],{"class":145},[100,988,989],{"class":106}," value ",[100,991,617],{"class":145},[100,993,620],{"class":106},[100,995,149],{"class":118},[100,997,998,1000,1002,1004],{"class":102,"line":152},[100,999,200],{"class":114},[100,1001,170],{"class":118},[100,1003,757],{"class":178},[100,1005,215],{"class":118},[14,1007,218],{},[91,1009,1010],{"className":93,"code":638,"language":95,"meta":96,"style":96},[17,1011,1012,1016],{"__ignoreMap":96},[100,1013,1014],{"class":102,"line":103},[100,1015,228],{"class":122},[100,1017,1018],{"class":102,"line":135},[100,1019,233],{"class":122},[14,1021,1022],{},"If your goal is just to loop through values, this is the easiest approach.",[14,1024,1025,1026,290],{},"For more general exception handling, see ",[295,1027,1029],{"href":1028},"\u002Fhow-to\u002Fhow-to-handle-exceptions-in-python","how to handle exceptions in Python",[77,1031,1033],{"id":1032},"generators-and-stopiteration","Generators and StopIteration",[14,1035,571,1036,1039],{},[22,1037,1038],{},"generator"," is a function that yields values one at a time.",[14,1041,1042],{},"Generators end in a normal way when:",[40,1044,1045,1048],{},[43,1046,1047],{},"they reach the end of the function",[43,1049,1050,1051],{},"they use ",[17,1052,1053],{},"return",[14,1055,1056,1057,1060,1061,1063],{},"Beginners should ",[22,1058,1059],{},"not"," manually raise ",[17,1062,19],{}," inside a generator.",[675,1065,1067],{"id":1066},"bad-example","Bad example",[91,1069,1071],{"className":93,"code":1070,"language":95,"meta":96,"style":96},"def count():\n    yield 1\n    raise StopIteration\n",[17,1072,1073,1086,1094],{"__ignoreMap":96},[100,1074,1075,1079,1083],{"class":102,"line":103},[100,1076,1078],{"class":1077},"sbsja","def",[100,1080,1082],{"class":1081},"sGLFI"," count",[100,1084,1085],{"class":118},"():\n",[100,1087,1088,1091],{"class":102,"line":135},[100,1089,1090],{"class":145},"    yield",[100,1092,1093],{"class":122}," 1\n",[100,1095,1096,1099],{"class":102,"line":142},[100,1097,1098],{"class":145},"    raise",[100,1100,1101],{"class":191}," StopIteration\n",[14,1103,1104],{},"In modern Python, this is not the right way to end a generator.",[675,1106,1108],{"id":1107},"good-example","Good example",[91,1110,1112],{"className":93,"code":1111,"language":95,"meta":96,"style":96},"def count():\n    yield 1\n    return\n",[17,1113,1114,1122,1128],{"__ignoreMap":96},[100,1115,1116,1118,1120],{"class":102,"line":103},[100,1117,1078],{"class":1077},[100,1119,1082],{"class":1081},[100,1121,1085],{"class":118},[100,1123,1124,1126],{"class":102,"line":135},[100,1125,1090],{"class":145},[100,1127,1093],{"class":122},[100,1129,1130],{"class":102,"line":142},[100,1131,1132],{"class":145},"    return\n",[14,1134,1135],{},"Or even more simply:",[91,1137,1139],{"className":93,"code":1138,"language":95,"meta":96,"style":96},"def count():\n    yield 1\n",[17,1140,1141,1149],{"__ignoreMap":96},[100,1142,1143,1145,1147],{"class":102,"line":103},[100,1144,1078],{"class":1077},[100,1146,1082],{"class":1081},[100,1148,1085],{"class":118},[100,1150,1151,1153],{"class":102,"line":135},[100,1152,1090],{"class":145},[100,1154,1093],{"class":122},[14,1156,1157],{},"When the generator finishes, Python handles the end correctly.",[14,1159,1160,1161,290],{},"If you want to learn more, see ",[295,1162,1164],{"href":1163},"\u002Flearn\u002Fgenerators-in-python-explained","generators in Python explained",[77,1166,1168],{"id":1167},"debugging-checklist","Debugging checklist",[14,1170,1171,1172,1174],{},"If you see ",[17,1173,19],{},", check these things:",[40,1176,1177,1183,1186,1189,1198],{},[43,1178,1179,1180,1182],{},"Check where ",[17,1181,48],{}," is called",[43,1184,1185],{},"Check how many values the iterator contains",[43,1187,1188],{},"Print values before the failing call",[43,1190,1191,1192,1194,1195,1197],{},"See if a ",[17,1193,71],{}," loop can replace manual ",[17,1196,48],{}," calls",[43,1199,1200,1201],{},"If using a generator, remove manual ",[17,1202,1203],{},"raise StopIteration",[14,1205,1206],{},"Useful debugging lines:",[91,1208,1210],{"className":93,"code":1209,"language":95,"meta":96,"style":96},"print(next(my_iter))\nprint(list(my_iter))\nprint(type(my_iter))\nprint(hasattr(my_iter, '__next__'))\n",[17,1211,1212,1227,1242,1257],{"__ignoreMap":96},[100,1213,1214,1216,1218,1220,1222,1225],{"class":102,"line":103},[100,1215,372],{"class":114},[100,1217,170],{"class":118},[100,1219,173],{"class":114},[100,1221,170],{"class":118},[100,1223,1224],{"class":178},"my_iter",[100,1226,182],{"class":118},[100,1228,1229,1231,1233,1236,1238,1240],{"class":102,"line":135},[100,1230,372],{"class":114},[100,1232,170],{"class":118},[100,1234,1235],{"class":191},"list",[100,1237,170],{"class":118},[100,1239,1224],{"class":178},[100,1241,182],{"class":118},[100,1243,1244,1246,1248,1251,1253,1255],{"class":102,"line":142},[100,1245,372],{"class":114},[100,1247,170],{"class":118},[100,1249,1250],{"class":191},"type",[100,1252,170],{"class":118},[100,1254,1224],{"class":178},[100,1256,182],{"class":118},[100,1258,1259,1261,1263,1266,1268,1270,1272,1275,1278,1281],{"class":102,"line":152},[100,1260,372],{"class":114},[100,1262,170],{"class":118},[100,1264,1265],{"class":114},"hasattr",[100,1267,170],{"class":118},[100,1269,1224],{"class":178},[100,1271,126],{"class":118},[100,1273,1274],{"class":205}," '",[100,1276,1277],{"class":209},"__next__",[100,1279,1280],{"class":205},"'",[100,1282,182],{"class":118},[14,1284,1285],{},"What these can help you find:",[40,1287,1288,1294,1300,1310],{},[43,1289,1290,1293],{},[17,1291,1292],{},"print(type(my_iter))"," shows what kind of object you are working with",[43,1295,1296,1299],{},[17,1297,1298],{},"print(hasattr(my_iter, '__next__'))"," checks if it behaves like an iterator",[43,1301,1302,1305,1306,1309],{},[17,1303,1304],{},"print(list(my_iter))"," shows remaining values, but it also ",[22,1307,1308],{},"consumes"," the iterator",[43,1311,1312,1315],{},[17,1313,1314],{},"print(next(my_iter))"," helps test one step at a time",[14,1317,1318],{},"Be careful with this line:",[91,1320,1322],{"className":93,"code":1321,"language":95,"meta":96,"style":96},"print(list(my_iter))\n",[17,1323,1324],{"__ignoreMap":96},[100,1325,1326,1328,1330,1332,1334,1336],{"class":102,"line":103},[100,1327,372],{"class":114},[100,1329,170],{"class":118},[100,1331,1235],{"class":191},[100,1333,170],{"class":118},[100,1335,1224],{"class":178},[100,1337,182],{"class":118},[14,1339,1340],{},"It uses up the remaining items. After that, the iterator may already be exhausted.",[77,1342,1344],{"id":1343},"common-causes","Common causes",[14,1346,1347],{},"These are the most common reasons this exception appears:",[40,1349,1350,1355,1364,1370],{},[43,1351,315,1352,1354],{},[17,1353,48],{}," after the iterator is exhausted",[43,1356,1357,1358,1360,1361,1363],{},"Using ",[17,1359,48],{}," in a ",[17,1362,55],{}," loop without handling the end",[43,1365,1366,1367,1369],{},"Manually raising ",[17,1368,19],{}," inside a generator",[43,1371,1372],{},"Reusing an iterator that has already been consumed",[675,1374,1376],{"id":1375},"reusing-a-consumed-iterator","Reusing a consumed iterator",[14,1378,1379],{},"This is another common mistake:",[91,1381,1383],{"className":93,"code":1382,"language":95,"meta":96,"style":96},"numbers = iter([1, 2, 3])\n\nfor n in numbers:\n    print(n)\n\nprint(\"Second loop:\")\n\nfor n in numbers:\n    print(n)\n",[17,1384,1385,1408,1412,1425,1436,1440,1455,1459,1471],{"__ignoreMap":96},[100,1386,1387,1389,1391,1393,1395,1397,1399,1401,1403,1406],{"class":102,"line":103},[100,1388,107],{"class":106},[100,1390,111],{"class":110},[100,1392,115],{"class":114},[100,1394,119],{"class":118},[100,1396,123],{"class":122},[100,1398,126],{"class":118},[100,1400,129],{"class":122},[100,1402,126],{"class":118},[100,1404,1405],{"class":122}," 3",[100,1407,132],{"class":118},[100,1409,1410],{"class":102,"line":135},[100,1411,139],{"emptyLinePlaceholder":138},[100,1413,1414,1416,1419,1421,1423],{"class":102,"line":142},[100,1415,71],{"class":145},[100,1417,1418],{"class":106}," n ",[100,1420,617],{"class":145},[100,1422,620],{"class":106},[100,1424,149],{"class":118},[100,1426,1427,1429,1431,1434],{"class":102,"line":152},[100,1428,200],{"class":114},[100,1430,170],{"class":118},[100,1432,1433],{"class":178},"n",[100,1435,215],{"class":118},[100,1437,1438],{"class":102,"line":164},[100,1439,139],{"emptyLinePlaceholder":138},[100,1441,1442,1444,1446,1448,1451,1453],{"class":102,"line":185},[100,1443,372],{"class":114},[100,1445,170],{"class":118},[100,1447,206],{"class":205},[100,1449,1450],{"class":209},"Second loop:",[100,1452,206],{"class":205},[100,1454,215],{"class":118},[100,1456,1457],{"class":102,"line":197},[100,1458,139],{"emptyLinePlaceholder":138},[100,1460,1461,1463,1465,1467,1469],{"class":102,"line":771},[100,1462,71],{"class":145},[100,1464,1418],{"class":106},[100,1466,617],{"class":145},[100,1468,620],{"class":106},[100,1470,149],{"class":118},[100,1472,1473,1475,1477,1479],{"class":102,"line":787},[100,1474,200],{"class":114},[100,1476,170],{"class":118},[100,1478,1433],{"class":178},[100,1480,215],{"class":118},[14,1482,218],{},[91,1484,1486],{"className":93,"code":1485,"language":95,"meta":96,"style":96},"1\n2\n3\nSecond loop:\n",[17,1487,1488,1492,1496,1501],{"__ignoreMap":96},[100,1489,1490],{"class":102,"line":103},[100,1491,228],{"class":122},[100,1493,1494],{"class":102,"line":135},[100,1495,233],{"class":122},[100,1497,1498],{"class":102,"line":142},[100,1499,1500],{"class":122},"3\n",[100,1502,1503,1506],{"class":102,"line":152},[100,1504,1505],{"class":106},"Second loop",[100,1507,149],{"class":118},[14,1509,1510],{},"The second loop prints nothing because the iterator was already consumed in the first loop.",[77,1512,1514],{"id":1513},"faq","FAQ",[675,1516,1518],{"id":1517},"is-stopiteration-always-an-error","Is StopIteration always an error?",[14,1520,1521],{},"No. It is a normal signal that an iterator is finished. It becomes a visible error only when your code does not handle it.",[675,1523,1525],{"id":1524},"why-does-a-for-loop-not-show-stopiteration","Why does a for loop not show StopIteration?",[14,1527,1528],{},"Because Python handles it internally to stop the loop.",[675,1530,1532],{"id":1531},"how-do-i-avoid-stopiteration-with-next","How do I avoid StopIteration with next()?",[14,1534,241,1535,1537,1538,290],{},[17,1536,244],{}," or pass a default value like ",[17,1539,1540],{},"next(my_iter, None)",[675,1542,1544],{"id":1543},"should-i-raise-stopiteration-in-a-generator","Should I raise StopIteration in a generator?",[14,1546,1547,1548,1550],{},"No. In generators, use ",[17,1549,1053],{}," or let the function end naturally.",[77,1552,1554],{"id":1553},"see-also","See also",[40,1556,1557,1562,1567,1577,1582],{},[43,1558,1559],{},[295,1560,1561],{"href":297},"Iterators and iterable objects explained",[43,1563,1564],{},[295,1565,1566],{"href":1163},"Generators in Python explained",[43,1568,1569],{},[295,1570,1572,1573,1576],{"href":1571},"\u002Freference\u002Fpython-enumerate-function-explained","Python ",[17,1574,1575],{},"enumerate()"," function explained",[43,1578,1579],{},[295,1580,1581],{"href":1028},"How to handle exceptions in Python",[43,1583,1584],{},[295,1585,1587],{"href":1586},"\u002Flearn\u002Fpython-errors-and-exceptions-explained","Python errors and exceptions explained",[1589,1590,1591],"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 .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--shiki-default:#005CC5;--shiki-dark:#79B8FF}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 .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 .s39Yj, html code.shiki .s39Yj{--shiki-light:#39ADB5;--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 .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--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 .s_sjI, html code.shiki .s_sjI{--shiki-light:#91B859;--shiki-default:#032F62;--shiki-dark:#9ECBFF}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 .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 pre.shiki code .s_hVV, html code.shiki .s_hVV{--shiki-light:#90A4AE;--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 .sGLFI, html code.shiki .sGLFI{--shiki-light:#6182B8;--shiki-default:#6F42C1;--shiki-dark:#B392F0}",{"title":96,"searchDepth":135,"depth":135,"links":1593},[1594,1595,1596,1597,1598,1599,1607,1611,1612,1615,1621],{"id":79,"depth":135,"text":80},{"id":257,"depth":135,"text":258},{"id":301,"depth":135,"text":302},{"id":421,"depth":135,"text":422},{"id":567,"depth":135,"text":568},{"id":672,"depth":135,"text":673,"children":1600},[1601,1603,1605],{"id":677,"depth":142,"text":1602},"Wrap next() in try-except",{"id":813,"depth":142,"text":1604},"Use a default value with next()",{"id":942,"depth":142,"text":1606},"Stop calling next() when the iterator is finished",{"id":1032,"depth":135,"text":1033,"children":1608},[1609,1610],{"id":1066,"depth":142,"text":1067},{"id":1107,"depth":142,"text":1108},{"id":1167,"depth":135,"text":1168},{"id":1343,"depth":135,"text":1344,"children":1613},[1614],{"id":1375,"depth":142,"text":1376},{"id":1513,"depth":135,"text":1514,"children":1616},[1617,1618,1619,1620],{"id":1517,"depth":142,"text":1518},{"id":1524,"depth":142,"text":1525},{"id":1531,"depth":142,"text":1532},{"id":1543,"depth":142,"text":1544},{"id":1553,"depth":135,"text":1554},"Master stopiteration exception in python explained in our comprehensive Python beginner guide.","md",{},"\u002Ferrors\u002Fstopiteration-exception-in-python-explained",{"title":5,"description":1622},"errors\u002Fstopiteration-exception-in-python-explained","NCvwaHOAt9Z-uYbMs0WX7idt19reaBIdNtR1vsWaQfM",1777585497267]