[{"data":1,"prerenderedAt":2898},["ShallowReactive",2],{"doc-\u002Ferrors\u002Findexerror-list-index-out-of-range-fix-explained":3},{"id":4,"title":5,"body":6,"description":2891,"extension":2892,"meta":2893,"navigation":131,"path":2894,"seo":2895,"stem":2896,"__hash__":2897},"content\u002Ferrors\u002Findexerror-list-index-out-of-range-fix-explained.md","IndexError: list index out of range (Fix Explained)",{"type":7,"value":8,"toc":2833},"minimark",[9,13,21,24,40,51,56,211,217,237,242,251,257,261,264,267,308,311,338,344,349,356,381,384,392,396,399,451,454,476,480,483,497,504,516,520,523,527,572,578,582,585,597,600,643,648,656,659,749,752,770,775,785,789,817,820,824,831,895,901,905,908,915,1020,1023,1027,1030,1111,1115,1141,1146,1150,1153,1218,1221,1228,1235,1295,1306,1310,1313,1488,1491,1495,1498,1502,1565,1573,1670,1676,1680,1773,1777,1856,1859,1863,1866,1870,1886,1889,1893,1912,1918,1922,1937,1940,1944,1975,1980,1984,1990,1993,2038,2041,2080,2090,2094,2097,2101,2163,2166,2172,2251,2255,2277,2280,2284,2352,2356,2362,2417,2421,2456,2459,2463,2466,2497,2504,2516,2519,2576,2589,2593,2597,2609,2613,2622,2626,2629,2668,2672,2675,2678,2773,2776,2780,2829],[10,11,5],"h1",{"id":12},"indexerror-list-index-out-of-range-fix-explained",[14,15,16,20],"p",{},[17,18,19],"code",{},"IndexError: list index out of range"," means your code is trying to read a list position that does not exist.",[14,22,23],{},"This usually happens when:",[25,26,27,31,34,37],"ul",{},[28,29,30],"li",{},"the index is too large",[28,32,33],{},"the list is empty",[28,35,36],{},"a loop goes one step too far",[28,38,39],{},"you expect more items than are really in the list",[14,41,42,43,50],{},"The good news is that this error is usually easy to fix. In most cases, you need to check the list length with ",[44,45,47],"a",{"href":46},"\u002Freference\u002Fpython-list-length-len\u002F",[17,48,49],{},"len()"," or use a safer looping pattern.",[52,53,55],"h2",{"id":54},"quick-fix","Quick fix",[57,58,63],"pre",{"className":59,"code":60,"language":61,"meta":62,"style":62},"language-python shiki shiki-themes material-theme-lighter github-light github-dark","items = [\"a\", \"b\", \"c\"]\nindex = 2\n\nif 0 \u003C= index \u003C len(items):\n    print(items[index])\nelse:\n    print(\"Index is out of range\")\n","python","",[17,64,65,114,126,133,166,185,194],{"__ignoreMap":62},[66,67,70,74,78,82,86,89,91,94,97,100,102,104,106,109,111],"span",{"class":68,"line":69},"line",1,[66,71,73],{"class":72},"su5hD","items ",[66,75,77],{"class":76},"smGrS","=",[66,79,81],{"class":80},"sP7_E"," [",[66,83,85],{"class":84},"sjJ54","\"",[66,87,44],{"class":88},"s_sjI",[66,90,85],{"class":84},[66,92,93],{"class":80},",",[66,95,96],{"class":84}," \"",[66,98,99],{"class":88},"b",[66,101,85],{"class":84},[66,103,93],{"class":80},[66,105,96],{"class":84},[66,107,108],{"class":88},"c",[66,110,85],{"class":84},[66,112,113],{"class":80},"]\n",[66,115,117,120,122],{"class":68,"line":116},2,[66,118,119],{"class":72},"index ",[66,121,77],{"class":76},[66,123,125],{"class":124},"srdBf"," 2\n",[66,127,129],{"class":68,"line":128},3,[66,130,132],{"emptyLinePlaceholder":131},true,"\n",[66,134,136,140,143,146,149,152,156,159,163],{"class":68,"line":135},4,[66,137,139],{"class":138},"sVHd0","if",[66,141,142],{"class":124}," 0",[66,144,145],{"class":76}," \u003C=",[66,147,148],{"class":72}," index ",[66,150,151],{"class":76},"\u003C",[66,153,155],{"class":154},"sptTA"," len",[66,157,158],{"class":80},"(",[66,160,162],{"class":161},"slqww","items",[66,164,165],{"class":80},"):\n",[66,167,169,172,174,176,179,182],{"class":68,"line":168},5,[66,170,171],{"class":154},"    print",[66,173,158],{"class":80},[66,175,162],{"class":161},[66,177,178],{"class":80},"[",[66,180,181],{"class":161},"index",[66,183,184],{"class":80},"])\n",[66,186,188,191],{"class":68,"line":187},6,[66,189,190],{"class":138},"else",[66,192,193],{"class":80},":\n",[66,195,197,199,201,203,206,208],{"class":68,"line":196},7,[66,198,171],{"class":154},[66,200,158],{"class":80},[66,202,85],{"class":84},[66,204,205],{"class":88},"Index is out of range",[66,207,85],{"class":84},[66,209,210],{"class":80},")\n",[14,212,213],{},[214,215,216],"strong",{},"What this does:",[25,218,219,225,231,234],{},[28,220,221,224],{},[17,222,223],{},"len(items)"," gets the number of items in the list",[28,226,227,230],{},[17,228,229],{},"0 \u003C= index \u003C len(items)"," checks whether the index is valid",[28,232,233],{},"If the index is valid, Python reads the item",[28,235,236],{},"If not, the program avoids the error",[14,238,239],{},[214,240,241],{},"Output:",[57,243,245],{"className":59,"code":244,"language":61,"meta":62,"style":62},"c\n",[17,246,247],{"__ignoreMap":62},[66,248,249],{"class":68,"line":69},[66,250,244],{"class":72},[14,252,253,254,256],{},"Use ",[17,255,49],{}," to make sure the index exists before reading from the list.",[52,258,260],{"id":259},"what-this-error-means","What this error means",[14,262,263],{},"A list index is a position in a list.",[14,265,266],{},"For example, in this list:",[57,268,270],{"className":59,"code":269,"language":61,"meta":62,"style":62},"colors = [\"red\", \"green\", \"blue\"]\n",[17,271,272],{"__ignoreMap":62},[66,273,274,277,279,281,283,286,288,290,292,295,297,299,301,304,306],{"class":68,"line":69},[66,275,276],{"class":72},"colors ",[66,278,77],{"class":76},[66,280,81],{"class":80},[66,282,85],{"class":84},[66,284,285],{"class":88},"red",[66,287,85],{"class":84},[66,289,93],{"class":80},[66,291,96],{"class":84},[66,293,294],{"class":88},"green",[66,296,85],{"class":84},[66,298,93],{"class":80},[66,300,96],{"class":84},[66,302,303],{"class":88},"blue",[66,305,85],{"class":84},[66,307,113],{"class":80},[14,309,310],{},"The indexes are:",[25,312,313,322,330],{},[28,314,315,318,319],{},[17,316,317],{},"0"," → ",[17,320,321],{},"\"red\"",[28,323,324,318,327],{},[17,325,326],{},"1",[17,328,329],{},"\"green\"",[28,331,332,318,335],{},[17,333,334],{},"2",[17,336,337],{},"\"blue\"",[14,339,340,341,343],{},"Python raises ",[17,342,19],{}," when you ask for a position that is not in the list.",[345,346,348],"h3",{"id":347},"valid-list-indexes","Valid list indexes",[14,350,351,352,355],{},"For a list with length ",[17,353,354],{},"3",":",[25,357,358,369],{},[28,359,360,361,363,364,366,367],{},"valid positive indexes are ",[17,362,317],{},", ",[17,365,326],{},", and ",[17,368,334],{},[28,370,371,372,363,375,366,378],{},"valid negative indexes are ",[17,373,374],{},"-1",[17,376,377],{},"-2",[17,379,380],{},"-3",[14,382,383],{},"Anything outside that range will fail.",[14,385,386,387,391],{},"If you need a refresher on list basics, see ",[44,388,390],{"href":389},"\u002Flearn\u002Fpython-lists-explained-beginner-guide\u002F","Python lists explained for beginners",".",[52,393,395],{"id":394},"simple-example-that-causes-the-error","Simple example that causes the error",[14,397,398],{},"Here is a short list with 3 items:",[57,400,402],{"className":59,"code":401,"language":61,"meta":62,"style":62},"items = [\"a\", \"b\", \"c\"]\nprint(items[3])\n",[17,403,404,436],{"__ignoreMap":62},[66,405,406,408,410,412,414,416,418,420,422,424,426,428,430,432,434],{"class":68,"line":69},[66,407,73],{"class":72},[66,409,77],{"class":76},[66,411,81],{"class":80},[66,413,85],{"class":84},[66,415,44],{"class":88},[66,417,85],{"class":84},[66,419,93],{"class":80},[66,421,96],{"class":84},[66,423,99],{"class":88},[66,425,85],{"class":84},[66,427,93],{"class":80},[66,429,96],{"class":84},[66,431,108],{"class":88},[66,433,85],{"class":84},[66,435,113],{"class":80},[66,437,438,441,443,445,447,449],{"class":68,"line":116},[66,439,440],{"class":154},"print",[66,442,158],{"class":80},[66,444,162],{"class":161},[66,446,178],{"class":80},[66,448,354],{"class":124},[66,450,184],{"class":80},[14,452,453],{},"This will raise:",[57,455,457],{"className":59,"code":456,"language":61,"meta":62,"style":62},"IndexError: list index out of range\n",[17,458,459],{"__ignoreMap":62},[66,460,461,465,467,470,473],{"class":68,"line":69},[66,462,464],{"class":463},"sZMiF","IndexError",[66,466,355],{"class":80},[66,468,469],{"class":463}," list",[66,471,472],{"class":72}," index out of ",[66,474,475],{"class":154},"range\n",[345,477,479],{"id":478},"why-this-fails","Why this fails",[14,481,482],{},"The list has 3 items, but its valid indexes are:",[25,484,485,489,493],{},[28,486,487],{},[17,488,317],{},[28,490,491],{},[17,492,326],{},[28,494,495],{},[17,496,334],{},[14,498,499,500,503],{},"So ",[17,501,502],{},"items[3]"," does not exist.",[14,505,506,507,363,509,366,511,513,514,391],{},"A common beginner mistake is thinking that a 3-item list has indexes ",[17,508,326],{},[17,510,334],{},[17,512,354],{},". In Python, list indexes start at ",[17,515,317],{},[52,517,519],{"id":518},"why-it-happens","Why it happens",[14,521,522],{},"This error can happen in several common situations.",[345,524,526],{"id":525},"using-an-index-that-is-too-large","Using an index that is too large",[57,528,530],{"className":59,"code":529,"language":61,"meta":62,"style":62},"numbers = [10, 20, 30]\nprint(numbers[5])\n",[17,531,532,556],{"__ignoreMap":62},[66,533,534,537,539,541,544,546,549,551,554],{"class":68,"line":69},[66,535,536],{"class":72},"numbers ",[66,538,77],{"class":76},[66,540,81],{"class":80},[66,542,543],{"class":124},"10",[66,545,93],{"class":80},[66,547,548],{"class":124}," 20",[66,550,93],{"class":80},[66,552,553],{"class":124}," 30",[66,555,113],{"class":80},[66,557,558,560,562,565,567,570],{"class":68,"line":116},[66,559,440],{"class":154},[66,561,158],{"class":80},[66,563,564],{"class":161},"numbers",[66,566,178],{"class":80},[66,568,569],{"class":124},"5",[66,571,184],{"class":80},[14,573,574,575,577],{},"Index ",[17,576,569],{}," is outside the list.",[345,579,581],{"id":580},"using-a-negative-index-that-goes-too-far","Using a negative index that goes too far",[14,583,584],{},"Negative indexes count from the end:",[25,586,587,592],{},[28,588,589,591],{},[17,590,374],{}," is the last item",[28,593,594,596],{},[17,595,377],{}," is the second-to-last item",[14,598,599],{},"But this still has limits:",[57,601,603],{"className":59,"code":602,"language":61,"meta":62,"style":62},"numbers = [10, 20, 30]\nprint(numbers[-4])\n",[17,604,605,625],{"__ignoreMap":62},[66,606,607,609,611,613,615,617,619,621,623],{"class":68,"line":69},[66,608,536],{"class":72},[66,610,77],{"class":76},[66,612,81],{"class":80},[66,614,543],{"class":124},[66,616,93],{"class":80},[66,618,548],{"class":124},[66,620,93],{"class":80},[66,622,553],{"class":124},[66,624,113],{"class":80},[66,626,627,629,631,633,635,638,641],{"class":68,"line":116},[66,628,440],{"class":154},[66,630,158],{"class":80},[66,632,564],{"class":161},[66,634,178],{"class":80},[66,636,637],{"class":76},"-",[66,639,640],{"class":124},"4",[66,642,184],{"class":80},[14,644,645,646,391],{},"This fails because the list only supports negative indexes down to ",[17,647,380],{},[345,649,651,652,655],{"id":650},"looping-with-the-wrong-range-stop-value","Looping with the wrong ",[17,653,654],{},"range()"," stop value",[14,657,658],{},"This is very common:",[57,660,662],{"className":59,"code":661,"language":61,"meta":62,"style":62},"items = [\"a\", \"b\", \"c\"]\n\nfor i in range(len(items) + 1):\n    print(items[i])\n",[17,663,664,696,700,734],{"__ignoreMap":62},[66,665,666,668,670,672,674,676,678,680,682,684,686,688,690,692,694],{"class":68,"line":69},[66,667,73],{"class":72},[66,669,77],{"class":76},[66,671,81],{"class":80},[66,673,85],{"class":84},[66,675,44],{"class":88},[66,677,85],{"class":84},[66,679,93],{"class":80},[66,681,96],{"class":84},[66,683,99],{"class":88},[66,685,85],{"class":84},[66,687,93],{"class":80},[66,689,96],{"class":84},[66,691,108],{"class":88},[66,693,85],{"class":84},[66,695,113],{"class":80},[66,697,698],{"class":68,"line":116},[66,699,132],{"emptyLinePlaceholder":131},[66,701,702,705,708,711,714,716,719,721,723,726,729,732],{"class":68,"line":128},[66,703,704],{"class":138},"for",[66,706,707],{"class":72}," i ",[66,709,710],{"class":138},"in",[66,712,713],{"class":154}," range",[66,715,158],{"class":80},[66,717,718],{"class":154},"len",[66,720,158],{"class":80},[66,722,162],{"class":161},[66,724,725],{"class":80},")",[66,727,728],{"class":76}," +",[66,730,731],{"class":124}," 1",[66,733,165],{"class":80},[66,735,736,738,740,742,744,747],{"class":68,"line":135},[66,737,171],{"class":154},[66,739,158],{"class":80},[66,741,162],{"class":161},[66,743,178],{"class":80},[66,745,746],{"class":161},"i",[66,748,184],{"class":80},[14,750,751],{},"This loop goes through:",[25,753,754,758,762,766],{},[28,755,756],{},[17,757,317],{},[28,759,760],{},[17,761,326],{},[28,763,764],{},[17,765,334],{},[28,767,768],{},[17,769,354],{},[14,771,574,772,774],{},[17,773,354],{}," does not exist, so the loop crashes at the end.",[14,776,777,778,391],{},"If you want to understand loop boundaries better, see ",[44,779,781,782,784],{"href":780},"\u002Freference\u002Fpython-range-function-explained\u002F","the Python ",[17,783,654],{}," function explained",[345,786,788],{"id":787},"accessing-a-list-before-checking-that-it-has-items","Accessing a list before checking that it has items",[57,790,792],{"className":59,"code":791,"language":61,"meta":62,"style":62},"items = []\nprint(items[0])\n",[17,793,794,803],{"__ignoreMap":62},[66,795,796,798,800],{"class":68,"line":69},[66,797,73],{"class":72},[66,799,77],{"class":76},[66,801,802],{"class":80}," []\n",[66,804,805,807,809,811,813,815],{"class":68,"line":116},[66,806,440],{"class":154},[66,808,158],{"class":80},[66,810,162],{"class":161},[66,812,178],{"class":80},[66,814,317],{"class":124},[66,816,184],{"class":80},[14,818,819],{},"This fails because the list is empty.",[345,821,823],{"id":822},"assuming-input-always-has-enough-items","Assuming input always has enough items",[14,825,826,827,830],{},"Sometimes the list comes from user input, ",[17,828,829],{},"split()",", a file, or an API.",[57,832,834],{"className":59,"code":833,"language":61,"meta":62,"style":62},"text = \"Alice\"\nparts = text.split(\",\")\n\nprint(parts[1])\n",[17,835,836,851,876,880],{"__ignoreMap":62},[66,837,838,841,843,845,848],{"class":68,"line":69},[66,839,840],{"class":72},"text ",[66,842,77],{"class":76},[66,844,96],{"class":84},[66,846,847],{"class":88},"Alice",[66,849,850],{"class":84},"\"\n",[66,852,853,856,858,861,863,866,868,870,872,874],{"class":68,"line":116},[66,854,855],{"class":72},"parts ",[66,857,77],{"class":76},[66,859,860],{"class":72}," text",[66,862,391],{"class":80},[66,864,865],{"class":161},"split",[66,867,158],{"class":80},[66,869,85],{"class":84},[66,871,93],{"class":88},[66,873,85],{"class":84},[66,875,210],{"class":80},[66,877,878],{"class":68,"line":128},[66,879,132],{"emptyLinePlaceholder":131},[66,881,882,884,886,889,891,893],{"class":68,"line":135},[66,883,440],{"class":154},[66,885,158],{"class":80},[66,887,888],{"class":161},"parts",[66,890,178],{"class":80},[66,892,326],{"class":124},[66,894,184],{"class":80},[14,896,897,898,900],{},"This fails because ",[17,899,888],{}," only has one item.",[52,902,904],{"id":903},"how-to-fix-it","How to fix it",[14,906,907],{},"There are several safe ways to fix this error.",[345,909,911,912,914],{"id":910},"_1-check-the-list-length-with-len-before-indexing","1. Check the list length with ",[17,913,49],{}," before indexing",[57,916,918],{"className":59,"code":917,"language":61,"meta":62,"style":62},"items = [\"a\", \"b\", \"c\"]\nindex = 1\n\nif 0 \u003C= index \u003C len(items):\n    print(items[index])\nelse:\n    print(\"Invalid index\")\n",[17,919,920,952,961,965,985,999,1005],{"__ignoreMap":62},[66,921,922,924,926,928,930,932,934,936,938,940,942,944,946,948,950],{"class":68,"line":69},[66,923,73],{"class":72},[66,925,77],{"class":76},[66,927,81],{"class":80},[66,929,85],{"class":84},[66,931,44],{"class":88},[66,933,85],{"class":84},[66,935,93],{"class":80},[66,937,96],{"class":84},[66,939,99],{"class":88},[66,941,85],{"class":84},[66,943,93],{"class":80},[66,945,96],{"class":84},[66,947,108],{"class":88},[66,949,85],{"class":84},[66,951,113],{"class":80},[66,953,954,956,958],{"class":68,"line":116},[66,955,119],{"class":72},[66,957,77],{"class":76},[66,959,960],{"class":124}," 1\n",[66,962,963],{"class":68,"line":128},[66,964,132],{"emptyLinePlaceholder":131},[66,966,967,969,971,973,975,977,979,981,983],{"class":68,"line":135},[66,968,139],{"class":138},[66,970,142],{"class":124},[66,972,145],{"class":76},[66,974,148],{"class":72},[66,976,151],{"class":76},[66,978,155],{"class":154},[66,980,158],{"class":80},[66,982,162],{"class":161},[66,984,165],{"class":80},[66,986,987,989,991,993,995,997],{"class":68,"line":168},[66,988,171],{"class":154},[66,990,158],{"class":80},[66,992,162],{"class":161},[66,994,178],{"class":80},[66,996,181],{"class":161},[66,998,184],{"class":80},[66,1000,1001,1003],{"class":68,"line":187},[66,1002,190],{"class":138},[66,1004,193],{"class":80},[66,1006,1007,1009,1011,1013,1016,1018],{"class":68,"line":196},[66,1008,171],{"class":154},[66,1010,158],{"class":80},[66,1012,85],{"class":84},[66,1014,1015],{"class":88},"Invalid index",[66,1017,85],{"class":84},[66,1019,210],{"class":80},[14,1021,1022],{},"This is one of the clearest fixes for beginners.",[345,1024,1026],{"id":1025},"_2-use-the-correct-loop-range","2. Use the correct loop range",[14,1028,1029],{},"If you really need indexes, use:",[57,1031,1033],{"className":59,"code":1032,"language":61,"meta":62,"style":62},"items = [\"a\", \"b\", \"c\"]\n\nfor i in range(len(items)):\n    print(i, items[i])\n",[17,1034,1035,1067,1071,1092],{"__ignoreMap":62},[66,1036,1037,1039,1041,1043,1045,1047,1049,1051,1053,1055,1057,1059,1061,1063,1065],{"class":68,"line":69},[66,1038,73],{"class":72},[66,1040,77],{"class":76},[66,1042,81],{"class":80},[66,1044,85],{"class":84},[66,1046,44],{"class":88},[66,1048,85],{"class":84},[66,1050,93],{"class":80},[66,1052,96],{"class":84},[66,1054,99],{"class":88},[66,1056,85],{"class":84},[66,1058,93],{"class":80},[66,1060,96],{"class":84},[66,1062,108],{"class":88},[66,1064,85],{"class":84},[66,1066,113],{"class":80},[66,1068,1069],{"class":68,"line":116},[66,1070,132],{"emptyLinePlaceholder":131},[66,1072,1073,1075,1077,1079,1081,1083,1085,1087,1089],{"class":68,"line":128},[66,1074,704],{"class":138},[66,1076,707],{"class":72},[66,1078,710],{"class":138},[66,1080,713],{"class":154},[66,1082,158],{"class":80},[66,1084,718],{"class":154},[66,1086,158],{"class":80},[66,1088,162],{"class":161},[66,1090,1091],{"class":80},")):\n",[66,1093,1094,1096,1098,1100,1102,1105,1107,1109],{"class":68,"line":135},[66,1095,171],{"class":154},[66,1097,158],{"class":80},[66,1099,746],{"class":161},[66,1101,93],{"class":80},[66,1103,1104],{"class":161}," items",[66,1106,178],{"class":80},[66,1108,746],{"class":161},[66,1110,184],{"class":80},[14,1112,1113],{},[214,1114,241],{},[57,1116,1118],{"className":59,"code":1117,"language":61,"meta":62,"style":62},"0 a\n1 b\n2 c\n",[17,1119,1120,1127,1134],{"__ignoreMap":62},[66,1121,1122,1124],{"class":68,"line":69},[66,1123,317],{"class":124},[66,1125,1126],{"class":72}," a\n",[66,1128,1129,1131],{"class":68,"line":116},[66,1130,326],{"class":124},[66,1132,1133],{"class":72}," b\n",[66,1135,1136,1138],{"class":68,"line":128},[66,1137,334],{"class":124},[66,1139,1140],{"class":72}," c\n",[14,1142,1143,1144,391],{},"Notice that the loop stops before ",[17,1145,223],{},[345,1147,1149],{"id":1148},"_3-prefer-direct-iteration-when-possible","3. Prefer direct iteration when possible",[14,1151,1152],{},"In many cases, you do not need indexes at all.",[57,1154,1156],{"className":59,"code":1155,"language":61,"meta":62,"style":62},"items = [\"a\", \"b\", \"c\"]\n\nfor item in items:\n    print(item)\n",[17,1157,1158,1190,1194,1207],{"__ignoreMap":62},[66,1159,1160,1162,1164,1166,1168,1170,1172,1174,1176,1178,1180,1182,1184,1186,1188],{"class":68,"line":69},[66,1161,73],{"class":72},[66,1163,77],{"class":76},[66,1165,81],{"class":80},[66,1167,85],{"class":84},[66,1169,44],{"class":88},[66,1171,85],{"class":84},[66,1173,93],{"class":80},[66,1175,96],{"class":84},[66,1177,99],{"class":88},[66,1179,85],{"class":84},[66,1181,93],{"class":80},[66,1183,96],{"class":84},[66,1185,108],{"class":88},[66,1187,85],{"class":84},[66,1189,113],{"class":80},[66,1191,1192],{"class":68,"line":116},[66,1193,132],{"emptyLinePlaceholder":131},[66,1195,1196,1198,1201,1203,1205],{"class":68,"line":128},[66,1197,704],{"class":138},[66,1199,1200],{"class":72}," item ",[66,1202,710],{"class":138},[66,1204,1104],{"class":72},[66,1206,193],{"class":80},[66,1208,1209,1211,1213,1216],{"class":68,"line":135},[66,1210,171],{"class":154},[66,1212,158],{"class":80},[66,1214,1215],{"class":161},"item",[66,1217,210],{"class":80},[14,1219,1220],{},"This is often safer and simpler.",[14,1222,1223,1224,391],{},"For more help, see ",[44,1225,1227],{"href":1226},"\u002Fhow-to\u002Fhow-to-loop-through-a-list-in-python\u002F","how to loop through a list in Python",[345,1229,1231,1232],{"id":1230},"_4-check-for-an-empty-list-before-using-my_list0","4. Check for an empty list before using ",[17,1233,1234],{},"my_list[0]",[57,1236,1238],{"className":59,"code":1237,"language":61,"meta":62,"style":62},"items = []\n\nif items:\n    print(items[0])\nelse:\n    print(\"The list is empty\")\n",[17,1239,1240,1248,1252,1260,1274,1280],{"__ignoreMap":62},[66,1241,1242,1244,1246],{"class":68,"line":69},[66,1243,73],{"class":72},[66,1245,77],{"class":76},[66,1247,802],{"class":80},[66,1249,1250],{"class":68,"line":116},[66,1251,132],{"emptyLinePlaceholder":131},[66,1253,1254,1256,1258],{"class":68,"line":128},[66,1255,139],{"class":138},[66,1257,1104],{"class":72},[66,1259,193],{"class":80},[66,1261,1262,1264,1266,1268,1270,1272],{"class":68,"line":135},[66,1263,171],{"class":154},[66,1265,158],{"class":80},[66,1267,162],{"class":161},[66,1269,178],{"class":80},[66,1271,317],{"class":124},[66,1273,184],{"class":80},[66,1275,1276,1278],{"class":68,"line":168},[66,1277,190],{"class":138},[66,1279,193],{"class":80},[66,1281,1282,1284,1286,1288,1291,1293],{"class":68,"line":187},[66,1283,171],{"class":154},[66,1285,158],{"class":80},[66,1287,85],{"class":84},[66,1289,1290],{"class":88},"The list is empty",[66,1292,85],{"class":84},[66,1294,210],{"class":80},[14,1296,1297,1298,1301,1302,1305],{},"In Python, an empty list is treated as ",[17,1299,1300],{},"False",", so ",[17,1303,1304],{},"if items:"," is a simple emptiness check.",[345,1307,1309],{"id":1308},"_5-print-the-index-and-list-length-while-debugging","5. Print the index and list length while debugging",[14,1311,1312],{},"If you are not sure what is going wrong, print both values:",[57,1314,1316],{"className":59,"code":1315,"language":61,"meta":62,"style":62},"items = [\"a\", \"b\", \"c\"]\nindex = 3\n\nprint(\"List:\", items)\nprint(\"Length:\", len(items))\nprint(\"Index:\", index)\n\nif 0 \u003C= index \u003C len(items):\n    print(items[index])\nelse:\n    print(\"Index is out of range\")\n",[17,1317,1318,1350,1359,1363,1382,1406,1426,1430,1451,1466,1473],{"__ignoreMap":62},[66,1319,1320,1322,1324,1326,1328,1330,1332,1334,1336,1338,1340,1342,1344,1346,1348],{"class":68,"line":69},[66,1321,73],{"class":72},[66,1323,77],{"class":76},[66,1325,81],{"class":80},[66,1327,85],{"class":84},[66,1329,44],{"class":88},[66,1331,85],{"class":84},[66,1333,93],{"class":80},[66,1335,96],{"class":84},[66,1337,99],{"class":88},[66,1339,85],{"class":84},[66,1341,93],{"class":80},[66,1343,96],{"class":84},[66,1345,108],{"class":88},[66,1347,85],{"class":84},[66,1349,113],{"class":80},[66,1351,1352,1354,1356],{"class":68,"line":116},[66,1353,119],{"class":72},[66,1355,77],{"class":76},[66,1357,1358],{"class":124}," 3\n",[66,1360,1361],{"class":68,"line":128},[66,1362,132],{"emptyLinePlaceholder":131},[66,1364,1365,1367,1369,1371,1374,1376,1378,1380],{"class":68,"line":135},[66,1366,440],{"class":154},[66,1368,158],{"class":80},[66,1370,85],{"class":84},[66,1372,1373],{"class":88},"List:",[66,1375,85],{"class":84},[66,1377,93],{"class":80},[66,1379,1104],{"class":161},[66,1381,210],{"class":80},[66,1383,1384,1386,1388,1390,1393,1395,1397,1399,1401,1403],{"class":68,"line":168},[66,1385,440],{"class":154},[66,1387,158],{"class":80},[66,1389,85],{"class":84},[66,1391,1392],{"class":88},"Length:",[66,1394,85],{"class":84},[66,1396,93],{"class":80},[66,1398,155],{"class":154},[66,1400,158],{"class":80},[66,1402,162],{"class":161},[66,1404,1405],{"class":80},"))\n",[66,1407,1408,1410,1412,1414,1417,1419,1421,1424],{"class":68,"line":187},[66,1409,440],{"class":154},[66,1411,158],{"class":80},[66,1413,85],{"class":84},[66,1415,1416],{"class":88},"Index:",[66,1418,85],{"class":84},[66,1420,93],{"class":80},[66,1422,1423],{"class":161}," index",[66,1425,210],{"class":80},[66,1427,1428],{"class":68,"line":196},[66,1429,132],{"emptyLinePlaceholder":131},[66,1431,1433,1435,1437,1439,1441,1443,1445,1447,1449],{"class":68,"line":1432},8,[66,1434,139],{"class":138},[66,1436,142],{"class":124},[66,1438,145],{"class":76},[66,1440,148],{"class":72},[66,1442,151],{"class":76},[66,1444,155],{"class":154},[66,1446,158],{"class":80},[66,1448,162],{"class":161},[66,1450,165],{"class":80},[66,1452,1454,1456,1458,1460,1462,1464],{"class":68,"line":1453},9,[66,1455,171],{"class":154},[66,1457,158],{"class":80},[66,1459,162],{"class":161},[66,1461,178],{"class":80},[66,1463,181],{"class":161},[66,1465,184],{"class":80},[66,1467,1469,1471],{"class":68,"line":1468},10,[66,1470,190],{"class":138},[66,1472,193],{"class":80},[66,1474,1476,1478,1480,1482,1484,1486],{"class":68,"line":1475},11,[66,1477,171],{"class":154},[66,1479,158],{"class":80},[66,1481,85],{"class":84},[66,1483,205],{"class":88},[66,1485,85],{"class":84},[66,1487,210],{"class":80},[14,1489,1490],{},"This makes it easier to see the mismatch.",[52,1492,1494],{"id":1493},"common-real-examples","Common real examples",[14,1496,1497],{},"These are common ways beginners run into this error.",[345,1499,1501],{"id":1500},"reading-the-first-item-from-an-empty-list","Reading the first item from an empty list",[57,1503,1505],{"className":59,"code":1504,"language":61,"meta":62,"style":62},"results = []\n\nif results:\n    print(results[0])\nelse:\n    print(\"No results found\")\n",[17,1506,1507,1516,1520,1529,1544,1550],{"__ignoreMap":62},[66,1508,1509,1512,1514],{"class":68,"line":69},[66,1510,1511],{"class":72},"results ",[66,1513,77],{"class":76},[66,1515,802],{"class":80},[66,1517,1518],{"class":68,"line":116},[66,1519,132],{"emptyLinePlaceholder":131},[66,1521,1522,1524,1527],{"class":68,"line":128},[66,1523,139],{"class":138},[66,1525,1526],{"class":72}," results",[66,1528,193],{"class":80},[66,1530,1531,1533,1535,1538,1540,1542],{"class":68,"line":135},[66,1532,171],{"class":154},[66,1534,158],{"class":80},[66,1536,1537],{"class":161},"results",[66,1539,178],{"class":80},[66,1541,317],{"class":124},[66,1543,184],{"class":80},[66,1545,1546,1548],{"class":68,"line":168},[66,1547,190],{"class":138},[66,1549,193],{"class":80},[66,1551,1552,1554,1556,1558,1561,1563],{"class":68,"line":187},[66,1553,171],{"class":154},[66,1555,158],{"class":80},[66,1557,85],{"class":84},[66,1559,1560],{"class":88},"No results found",[66,1562,85],{"class":84},[66,1564,210],{"class":80},[345,1566,1568,1569,1572],{"id":1567},"using-i-1-near-the-end-of-a-loop","Using ",[17,1570,1571],{},"i + 1"," near the end of a loop",[57,1574,1576],{"className":59,"code":1575,"language":61,"meta":62,"style":62},"items = [\"a\", \"b\", \"c\"]\n\nfor i in range(len(items) - 1):\n    print(items[i], items[i + 1])\n",[17,1577,1578,1610,1614,1641],{"__ignoreMap":62},[66,1579,1580,1582,1584,1586,1588,1590,1592,1594,1596,1598,1600,1602,1604,1606,1608],{"class":68,"line":69},[66,1581,73],{"class":72},[66,1583,77],{"class":76},[66,1585,81],{"class":80},[66,1587,85],{"class":84},[66,1589,44],{"class":88},[66,1591,85],{"class":84},[66,1593,93],{"class":80},[66,1595,96],{"class":84},[66,1597,99],{"class":88},[66,1599,85],{"class":84},[66,1601,93],{"class":80},[66,1603,96],{"class":84},[66,1605,108],{"class":88},[66,1607,85],{"class":84},[66,1609,113],{"class":80},[66,1611,1612],{"class":68,"line":116},[66,1613,132],{"emptyLinePlaceholder":131},[66,1615,1616,1618,1620,1622,1624,1626,1628,1630,1632,1634,1637,1639],{"class":68,"line":128},[66,1617,704],{"class":138},[66,1619,707],{"class":72},[66,1621,710],{"class":138},[66,1623,713],{"class":154},[66,1625,158],{"class":80},[66,1627,718],{"class":154},[66,1629,158],{"class":80},[66,1631,162],{"class":161},[66,1633,725],{"class":80},[66,1635,1636],{"class":76}," -",[66,1638,731],{"class":124},[66,1640,165],{"class":80},[66,1642,1643,1645,1647,1649,1651,1653,1656,1658,1660,1663,1666,1668],{"class":68,"line":135},[66,1644,171],{"class":154},[66,1646,158],{"class":80},[66,1648,162],{"class":161},[66,1650,178],{"class":80},[66,1652,746],{"class":161},[66,1654,1655],{"class":80},"],",[66,1657,1104],{"class":161},[66,1659,178],{"class":80},[66,1661,1662],{"class":161},"i ",[66,1664,1665],{"class":76},"+",[66,1667,731],{"class":124},[66,1669,184],{"class":80},[14,1671,1672,1673,1675],{},"This works because the loop stops before ",[17,1674,1571],{}," goes out of range.",[345,1677,1679],{"id":1678},"splitting-text-and-expecting-more-parts-than-exist","Splitting text and expecting more parts than exist",[57,1681,1683],{"className":59,"code":1682,"language":61,"meta":62,"style":62},"name = \"Alice\"\nparts = name.split()\n\nif len(parts) >= 2:\n    print(parts[1])\nelse:\n    print(\"There is no second word\")\n",[17,1684,1685,1698,1714,1718,1738,1752,1758],{"__ignoreMap":62},[66,1686,1687,1690,1692,1694,1696],{"class":68,"line":69},[66,1688,1689],{"class":72},"name ",[66,1691,77],{"class":76},[66,1693,96],{"class":84},[66,1695,847],{"class":88},[66,1697,850],{"class":84},[66,1699,1700,1702,1704,1707,1709,1711],{"class":68,"line":116},[66,1701,855],{"class":72},[66,1703,77],{"class":76},[66,1705,1706],{"class":72}," name",[66,1708,391],{"class":80},[66,1710,865],{"class":161},[66,1712,1713],{"class":80},"()\n",[66,1715,1716],{"class":68,"line":128},[66,1717,132],{"emptyLinePlaceholder":131},[66,1719,1720,1722,1724,1726,1728,1730,1733,1736],{"class":68,"line":135},[66,1721,139],{"class":138},[66,1723,155],{"class":154},[66,1725,158],{"class":80},[66,1727,888],{"class":161},[66,1729,725],{"class":80},[66,1731,1732],{"class":76}," >=",[66,1734,1735],{"class":124}," 2",[66,1737,193],{"class":80},[66,1739,1740,1742,1744,1746,1748,1750],{"class":68,"line":168},[66,1741,171],{"class":154},[66,1743,158],{"class":80},[66,1745,888],{"class":161},[66,1747,178],{"class":80},[66,1749,326],{"class":124},[66,1751,184],{"class":80},[66,1753,1754,1756],{"class":68,"line":187},[66,1755,190],{"class":138},[66,1757,193],{"class":80},[66,1759,1760,1762,1764,1766,1769,1771],{"class":68,"line":196},[66,1761,171],{"class":154},[66,1763,158],{"class":80},[66,1765,85],{"class":84},[66,1767,1768],{"class":88},"There is no second word",[66,1770,85],{"class":84},[66,1772,210],{"class":80},[345,1774,1776],{"id":1775},"accessing-results-from-a-file-or-api-without-checking-length","Accessing results from a file or API without checking length",[57,1778,1780],{"className":59,"code":1779,"language":61,"meta":62,"style":62},"results = [\"first result\"]\n\nif len(results) >= 2:\n    print(results[1])\nelse:\n    print(\"There is no second result\")\n",[17,1781,1782,1799,1803,1821,1835,1841],{"__ignoreMap":62},[66,1783,1784,1786,1788,1790,1792,1795,1797],{"class":68,"line":69},[66,1785,1511],{"class":72},[66,1787,77],{"class":76},[66,1789,81],{"class":80},[66,1791,85],{"class":84},[66,1793,1794],{"class":88},"first result",[66,1796,85],{"class":84},[66,1798,113],{"class":80},[66,1800,1801],{"class":68,"line":116},[66,1802,132],{"emptyLinePlaceholder":131},[66,1804,1805,1807,1809,1811,1813,1815,1817,1819],{"class":68,"line":128},[66,1806,139],{"class":138},[66,1808,155],{"class":154},[66,1810,158],{"class":80},[66,1812,1537],{"class":161},[66,1814,725],{"class":80},[66,1816,1732],{"class":76},[66,1818,1735],{"class":124},[66,1820,193],{"class":80},[66,1822,1823,1825,1827,1829,1831,1833],{"class":68,"line":135},[66,1824,171],{"class":154},[66,1826,158],{"class":80},[66,1828,1537],{"class":161},[66,1830,178],{"class":80},[66,1832,326],{"class":124},[66,1834,184],{"class":80},[66,1836,1837,1839],{"class":68,"line":168},[66,1838,190],{"class":138},[66,1840,193],{"class":80},[66,1842,1843,1845,1847,1849,1852,1854],{"class":68,"line":187},[66,1844,171],{"class":154},[66,1846,158],{"class":80},[66,1848,85],{"class":84},[66,1850,1851],{"class":88},"There is no second result",[66,1853,85],{"class":84},[66,1855,210],{"class":80},[14,1857,1858],{},"When data comes from outside your program, always check its length first.",[52,1860,1862],{"id":1861},"debugging-steps","Debugging steps",[14,1864,1865],{},"If you are getting this error and do not know why, try these steps.",[345,1867,1869],{"id":1868},"_1-print-the-list","1. Print the list",[57,1871,1873],{"className":59,"code":1872,"language":61,"meta":62,"style":62},"print(my_list)\n",[17,1874,1875],{"__ignoreMap":62},[66,1876,1877,1879,1881,1884],{"class":68,"line":69},[66,1878,440],{"class":154},[66,1880,158],{"class":80},[66,1882,1883],{"class":161},"my_list",[66,1885,210],{"class":80},[14,1887,1888],{},"This shows what is actually in the list.",[345,1890,1892],{"id":1891},"_2-print-the-list-length","2. Print the list length",[57,1894,1896],{"className":59,"code":1895,"language":61,"meta":62,"style":62},"print(len(my_list))\n",[17,1897,1898],{"__ignoreMap":62},[66,1899,1900,1902,1904,1906,1908,1910],{"class":68,"line":69},[66,1901,440],{"class":154},[66,1903,158],{"class":80},[66,1905,718],{"class":154},[66,1907,158],{"class":80},[66,1909,1883],{"class":161},[66,1911,1405],{"class":80},[14,1913,1914,1915,391],{},"This tells you the highest valid positive index: ",[17,1916,1917],{},"len(my_list) - 1",[345,1919,1921],{"id":1920},"_3-print-the-index-value","3. Print the index value",[57,1923,1925],{"className":59,"code":1924,"language":61,"meta":62,"style":62},"print(index)\n",[17,1926,1927],{"__ignoreMap":62},[66,1928,1929,1931,1933,1935],{"class":68,"line":69},[66,1930,440],{"class":154},[66,1932,158],{"class":80},[66,1934,181],{"class":161},[66,1936,210],{"class":80},[14,1938,1939],{},"Sometimes the index is not what you expected.",[345,1941,1943],{"id":1942},"_4-check-whether-the-list-is-empty","4. Check whether the list is empty",[57,1945,1947],{"className":59,"code":1946,"language":61,"meta":62,"style":62},"if not my_list:\n    print(\"The list is empty\")\n",[17,1948,1949,1961],{"__ignoreMap":62},[66,1950,1951,1953,1956,1959],{"class":68,"line":69},[66,1952,139],{"class":138},[66,1954,1955],{"class":76}," not",[66,1957,1958],{"class":72}," my_list",[66,1960,193],{"class":80},[66,1962,1963,1965,1967,1969,1971,1973],{"class":68,"line":116},[66,1964,171],{"class":154},[66,1966,158],{"class":80},[66,1968,85],{"class":84},[66,1970,1290],{"class":88},[66,1972,85],{"class":84},[66,1974,210],{"class":80},[14,1976,1977,1978,391],{},"This is important before using ",[17,1979,1234],{},[345,1981,1983],{"id":1982},"_5-confirm-loop-boundaries","5. Confirm loop boundaries",[14,1985,1986,1987,1989],{},"If the error happens in a loop, check your ",[17,1988,654],{}," carefully.",[14,1991,1992],{},"Bad:",[57,1994,1996],{"className":59,"code":1995,"language":61,"meta":62,"style":62},"for i in range(len(my_list) + 1):\n    print(my_list[i])\n",[17,1997,1998,2024],{"__ignoreMap":62},[66,1999,2000,2002,2004,2006,2008,2010,2012,2014,2016,2018,2020,2022],{"class":68,"line":69},[66,2001,704],{"class":138},[66,2003,707],{"class":72},[66,2005,710],{"class":138},[66,2007,713],{"class":154},[66,2009,158],{"class":80},[66,2011,718],{"class":154},[66,2013,158],{"class":80},[66,2015,1883],{"class":161},[66,2017,725],{"class":80},[66,2019,728],{"class":76},[66,2021,731],{"class":124},[66,2023,165],{"class":80},[66,2025,2026,2028,2030,2032,2034,2036],{"class":68,"line":116},[66,2027,171],{"class":154},[66,2029,158],{"class":80},[66,2031,1883],{"class":161},[66,2033,178],{"class":80},[66,2035,746],{"class":161},[66,2037,184],{"class":80},[14,2039,2040],{},"Good:",[57,2042,2044],{"className":59,"code":2043,"language":61,"meta":62,"style":62},"for i in range(len(my_list)):\n    print(my_list[i])\n",[17,2045,2046,2066],{"__ignoreMap":62},[66,2047,2048,2050,2052,2054,2056,2058,2060,2062,2064],{"class":68,"line":69},[66,2049,704],{"class":138},[66,2051,707],{"class":72},[66,2053,710],{"class":138},[66,2055,713],{"class":154},[66,2057,158],{"class":80},[66,2059,718],{"class":154},[66,2061,158],{"class":80},[66,2063,1883],{"class":161},[66,2065,1091],{"class":80},[66,2067,2068,2070,2072,2074,2076,2078],{"class":68,"line":116},[66,2069,171],{"class":154},[66,2071,158],{"class":80},[66,2073,1883],{"class":161},[66,2075,178],{"class":80},[66,2077,746],{"class":161},[66,2079,184],{"class":80},[14,2081,2082,2083,2089],{},"If you need both the index and the value, ",[44,2084,2086],{"href":2085},"\u002Freference\u002Fpython-enumerate-function-explained\u002F",[17,2087,2088],{},"enumerate()"," is often a better choice.",[52,2091,2093],{"id":2092},"ways-to-avoid-this-error","Ways to avoid this error",[14,2095,2096],{},"These habits help prevent the problem before it happens.",[345,2098,2100],{"id":2099},"loop-over-items-instead-of-indexes","Loop over items instead of indexes",[57,2102,2103],{"className":59,"code":1155,"language":61,"meta":62,"style":62},[17,2104,2105,2137,2141,2153],{"__ignoreMap":62},[66,2106,2107,2109,2111,2113,2115,2117,2119,2121,2123,2125,2127,2129,2131,2133,2135],{"class":68,"line":69},[66,2108,73],{"class":72},[66,2110,77],{"class":76},[66,2112,81],{"class":80},[66,2114,85],{"class":84},[66,2116,44],{"class":88},[66,2118,85],{"class":84},[66,2120,93],{"class":80},[66,2122,96],{"class":84},[66,2124,99],{"class":88},[66,2126,85],{"class":84},[66,2128,93],{"class":80},[66,2130,96],{"class":84},[66,2132,108],{"class":88},[66,2134,85],{"class":84},[66,2136,113],{"class":80},[66,2138,2139],{"class":68,"line":116},[66,2140,132],{"emptyLinePlaceholder":131},[66,2142,2143,2145,2147,2149,2151],{"class":68,"line":128},[66,2144,704],{"class":138},[66,2146,1200],{"class":72},[66,2148,710],{"class":138},[66,2150,1104],{"class":72},[66,2152,193],{"class":80},[66,2154,2155,2157,2159,2161],{"class":68,"line":135},[66,2156,171],{"class":154},[66,2158,158],{"class":80},[66,2160,1215],{"class":161},[66,2162,210],{"class":80},[14,2164,2165],{},"This avoids manual index mistakes.",[345,2167,253,2169,2171],{"id":2168},"use-enumerate-when-you-need-both-index-and-value",[17,2170,2088],{}," when you need both index and value",[57,2173,2175],{"className":59,"code":2174,"language":61,"meta":62,"style":62},"items = [\"a\", \"b\", \"c\"]\n\nfor i, value in enumerate(items):\n    print(i, value)\n",[17,2176,2177,2209,2213,2236],{"__ignoreMap":62},[66,2178,2179,2181,2183,2185,2187,2189,2191,2193,2195,2197,2199,2201,2203,2205,2207],{"class":68,"line":69},[66,2180,73],{"class":72},[66,2182,77],{"class":76},[66,2184,81],{"class":80},[66,2186,85],{"class":84},[66,2188,44],{"class":88},[66,2190,85],{"class":84},[66,2192,93],{"class":80},[66,2194,96],{"class":84},[66,2196,99],{"class":88},[66,2198,85],{"class":84},[66,2200,93],{"class":80},[66,2202,96],{"class":84},[66,2204,108],{"class":88},[66,2206,85],{"class":84},[66,2208,113],{"class":80},[66,2210,2211],{"class":68,"line":116},[66,2212,132],{"emptyLinePlaceholder":131},[66,2214,2215,2217,2220,2222,2225,2227,2230,2232,2234],{"class":68,"line":128},[66,2216,704],{"class":138},[66,2218,2219],{"class":72}," i",[66,2221,93],{"class":80},[66,2223,2224],{"class":72}," value ",[66,2226,710],{"class":138},[66,2228,2229],{"class":154}," enumerate",[66,2231,158],{"class":80},[66,2233,162],{"class":161},[66,2235,165],{"class":80},[66,2237,2238,2240,2242,2244,2246,2249],{"class":68,"line":135},[66,2239,171],{"class":154},[66,2241,158],{"class":80},[66,2243,746],{"class":161},[66,2245,93],{"class":80},[66,2247,2248],{"class":161}," value",[66,2250,210],{"class":80},[14,2252,2253],{},[214,2254,241],{},[57,2256,2257],{"className":59,"code":1117,"language":61,"meta":62,"style":62},[17,2258,2259,2265,2271],{"__ignoreMap":62},[66,2260,2261,2263],{"class":68,"line":69},[66,2262,317],{"class":124},[66,2264,1126],{"class":72},[66,2266,2267,2269],{"class":68,"line":116},[66,2268,326],{"class":124},[66,2270,1133],{"class":72},[66,2272,2273,2275],{"class":68,"line":128},[66,2274,334],{"class":124},[66,2276,1140],{"class":72},[14,2278,2279],{},"This is safer than managing the index yourself in many cases.",[345,2281,2283],{"id":2282},"check-list-length-before-reading-fixed-positions","Check list length before reading fixed positions",[57,2285,2287],{"className":59,"code":2286,"language":61,"meta":62,"style":62},"items = [\"apple\", \"banana\"]\n\nif len(items) > 1:\n    print(items[1])\n",[17,2288,2289,2315,2319,2338],{"__ignoreMap":62},[66,2290,2291,2293,2295,2297,2299,2302,2304,2306,2308,2311,2313],{"class":68,"line":69},[66,2292,73],{"class":72},[66,2294,77],{"class":76},[66,2296,81],{"class":80},[66,2298,85],{"class":84},[66,2300,2301],{"class":88},"apple",[66,2303,85],{"class":84},[66,2305,93],{"class":80},[66,2307,96],{"class":84},[66,2309,2310],{"class":88},"banana",[66,2312,85],{"class":84},[66,2314,113],{"class":80},[66,2316,2317],{"class":68,"line":116},[66,2318,132],{"emptyLinePlaceholder":131},[66,2320,2321,2323,2325,2327,2329,2331,2334,2336],{"class":68,"line":128},[66,2322,139],{"class":138},[66,2324,155],{"class":154},[66,2326,158],{"class":80},[66,2328,162],{"class":161},[66,2330,725],{"class":80},[66,2332,2333],{"class":76}," >",[66,2335,731],{"class":124},[66,2337,193],{"class":80},[66,2339,2340,2342,2344,2346,2348,2350],{"class":68,"line":135},[66,2341,171],{"class":154},[66,2343,158],{"class":80},[66,2345,162],{"class":161},[66,2347,178],{"class":80},[66,2349,326],{"class":124},[66,2351,184],{"class":80},[345,2353,2355],{"id":2354},"use-slicing-when-a-missing-range-should-not-crash-the-program","Use slicing when a missing range should not crash the program",[14,2357,2358,2359,2361],{},"Slicing is often safer because it does not raise ",[17,2360,464],{}," when the end is too large.",[57,2363,2365],{"className":59,"code":2364,"language":61,"meta":62,"style":62},"items = [\"a\", \"b\", \"c\"]\nprint(items[0:5])\n",[17,2366,2367,2399],{"__ignoreMap":62},[66,2368,2369,2371,2373,2375,2377,2379,2381,2383,2385,2387,2389,2391,2393,2395,2397],{"class":68,"line":69},[66,2370,73],{"class":72},[66,2372,77],{"class":76},[66,2374,81],{"class":80},[66,2376,85],{"class":84},[66,2378,44],{"class":88},[66,2380,85],{"class":84},[66,2382,93],{"class":80},[66,2384,96],{"class":84},[66,2386,99],{"class":88},[66,2388,85],{"class":84},[66,2390,93],{"class":80},[66,2392,96],{"class":84},[66,2394,108],{"class":88},[66,2396,85],{"class":84},[66,2398,113],{"class":80},[66,2400,2401,2403,2405,2407,2409,2411,2413,2415],{"class":68,"line":116},[66,2402,440],{"class":154},[66,2404,158],{"class":80},[66,2406,162],{"class":161},[66,2408,178],{"class":80},[66,2410,317],{"class":124},[66,2412,355],{"class":80},[66,2414,569],{"class":124},[66,2416,184],{"class":80},[14,2418,2419],{},[214,2420,241],{},[57,2422,2424],{"className":59,"code":2423,"language":61,"meta":62,"style":62},"['a', 'b', 'c']\n",[17,2425,2426],{"__ignoreMap":62},[66,2427,2428,2430,2433,2435,2437,2439,2442,2444,2446,2448,2450,2452,2454],{"class":68,"line":69},[66,2429,178],{"class":80},[66,2431,2432],{"class":84},"'",[66,2434,44],{"class":88},[66,2436,2432],{"class":84},[66,2438,93],{"class":80},[66,2440,2441],{"class":84}," '",[66,2443,99],{"class":88},[66,2445,2432],{"class":84},[66,2447,93],{"class":80},[66,2449,2441],{"class":84},[66,2451,108],{"class":88},[66,2453,2432],{"class":84},[66,2455,113],{"class":80},[14,2457,2458],{},"That can be useful when you want a range of items without crashing.",[52,2460,2462],{"id":2461},"common-mistakes","Common mistakes",[14,2464,2465],{},"These are some very common causes of this error:",[25,2467,2468,2474,2479,2485,2491],{},[28,2469,2470,2471],{},"Trying to access ",[17,2472,2473],{},"my_list[len(my_list)]",[28,2475,1568,2476],{},[17,2477,2478],{},"range(len(my_list) + 1)",[28,2480,2481,2482,2484],{},"Reading ",[17,2483,1234],{}," when the list is empty",[28,2486,2487,2488],{},"Using a negative index smaller than ",[17,2489,2490],{},"-len(my_list)",[28,2492,2493,2494,2496],{},"Expecting ",[17,2495,829],{}," to return more items than it did",[345,2498,2500,2501,2503],{"id":2499},"why-my_listlenmy_list-is-wrong","Why ",[17,2502,2473],{}," is wrong",[14,2505,2506,2507,2509,2510,363,2512,366,2514,391],{},"If a list has length ",[17,2508,354],{},", the valid indexes are ",[17,2511,317],{},[17,2513,326],{},[17,2515,334],{},[14,2517,2518],{},"So this is wrong:",[57,2520,2522],{"className":59,"code":2521,"language":61,"meta":62,"style":62},"my_list = [\"a\", \"b\", \"c\"]\nprint(my_list[len(my_list)])\n",[17,2523,2524,2557],{"__ignoreMap":62},[66,2525,2526,2529,2531,2533,2535,2537,2539,2541,2543,2545,2547,2549,2551,2553,2555],{"class":68,"line":69},[66,2527,2528],{"class":72},"my_list ",[66,2530,77],{"class":76},[66,2532,81],{"class":80},[66,2534,85],{"class":84},[66,2536,44],{"class":88},[66,2538,85],{"class":84},[66,2540,93],{"class":80},[66,2542,96],{"class":84},[66,2544,99],{"class":88},[66,2546,85],{"class":84},[66,2548,93],{"class":80},[66,2550,96],{"class":84},[66,2552,108],{"class":88},[66,2554,85],{"class":84},[66,2556,113],{"class":80},[66,2558,2559,2561,2563,2565,2567,2569,2571,2573],{"class":68,"line":116},[66,2560,440],{"class":154},[66,2562,158],{"class":80},[66,2564,1883],{"class":161},[66,2566,178],{"class":80},[66,2568,718],{"class":154},[66,2570,158],{"class":80},[66,2572,1883],{"class":161},[66,2574,2575],{"class":80},")])\n",[14,2577,2578,2579,2582,2583,2585,2586,2588],{},"Because ",[17,2580,2581],{},"len(my_list)"," is ",[17,2584,354],{},", and index ",[17,2587,354],{}," is out of range.",[52,2590,2592],{"id":2591},"faq","FAQ",[345,2594,2596],{"id":2595},"why-does-index-3-fail-in-a-list-with-3-items","Why does index 3 fail in a list with 3 items?",[14,2598,2599,2600,2602,2603,363,2605,366,2607,391],{},"Because list indexes start at ",[17,2601,317],{},". A 3-item list has valid indexes ",[17,2604,317],{},[17,2606,326],{},[17,2608,334],{},[345,2610,2612],{"id":2611},"can-negative-indexes-cause-this-error","Can negative indexes cause this error?",[14,2614,2615,2616,2618,2619,2621],{},"Yes. Negative indexes work only if they stay within the list length, such as ",[17,2617,374],{}," or ",[17,2620,377],{}," for a non-empty list.",[345,2623,2625],{"id":2624},"how-do-i-check-if-an-index-is-valid","How do I check if an index is valid?",[14,2627,2628],{},"Use a condition like this before accessing the item:",[57,2630,2632],{"className":59,"code":2631,"language":61,"meta":62,"style":62},"if 0 \u003C= index \u003C len(my_list):\n    print(my_list[index])\n",[17,2633,2634,2654],{"__ignoreMap":62},[66,2635,2636,2638,2640,2642,2644,2646,2648,2650,2652],{"class":68,"line":69},[66,2637,139],{"class":138},[66,2639,142],{"class":124},[66,2641,145],{"class":76},[66,2643,148],{"class":72},[66,2645,151],{"class":76},[66,2647,155],{"class":154},[66,2649,158],{"class":80},[66,2651,1883],{"class":161},[66,2653,165],{"class":80},[66,2655,2656,2658,2660,2662,2664,2666],{"class":68,"line":116},[66,2657,171],{"class":154},[66,2659,158],{"class":80},[66,2661,1883],{"class":161},[66,2663,178],{"class":80},[66,2665,181],{"class":161},[66,2667,184],{"class":80},[345,2669,2671],{"id":2670},"should-i-use-try-except-for-this","Should I use try-except for this?",[14,2673,2674],{},"You can, but for beginners it is often clearer to check the length first when missing items are expected.",[14,2676,2677],{},"For example:",[57,2679,2681],{"className":59,"code":2680,"language":61,"meta":62,"style":62},"items = [\"a\", \"b\", \"c\"]\nindex = 5\n\ntry:\n    print(items[index])\nexcept IndexError:\n    print(\"Index is out of range\")\n",[17,2682,2683,2715,2724,2728,2735,2749,2759],{"__ignoreMap":62},[66,2684,2685,2687,2689,2691,2693,2695,2697,2699,2701,2703,2705,2707,2709,2711,2713],{"class":68,"line":69},[66,2686,73],{"class":72},[66,2688,77],{"class":76},[66,2690,81],{"class":80},[66,2692,85],{"class":84},[66,2694,44],{"class":88},[66,2696,85],{"class":84},[66,2698,93],{"class":80},[66,2700,96],{"class":84},[66,2702,99],{"class":88},[66,2704,85],{"class":84},[66,2706,93],{"class":80},[66,2708,96],{"class":84},[66,2710,108],{"class":88},[66,2712,85],{"class":84},[66,2714,113],{"class":80},[66,2716,2717,2719,2721],{"class":68,"line":116},[66,2718,119],{"class":72},[66,2720,77],{"class":76},[66,2722,2723],{"class":124}," 5\n",[66,2725,2726],{"class":68,"line":128},[66,2727,132],{"emptyLinePlaceholder":131},[66,2729,2730,2733],{"class":68,"line":135},[66,2731,2732],{"class":138},"try",[66,2734,193],{"class":80},[66,2736,2737,2739,2741,2743,2745,2747],{"class":68,"line":168},[66,2738,171],{"class":154},[66,2740,158],{"class":80},[66,2742,162],{"class":161},[66,2744,178],{"class":80},[66,2746,181],{"class":161},[66,2748,184],{"class":80},[66,2750,2751,2754,2757],{"class":68,"line":187},[66,2752,2753],{"class":138},"except",[66,2755,2756],{"class":463}," IndexError",[66,2758,193],{"class":80},[66,2760,2761,2763,2765,2767,2769,2771],{"class":68,"line":196},[66,2762,171],{"class":154},[66,2764,158],{"class":80},[66,2766,85],{"class":84},[66,2768,205],{"class":88},[66,2770,85],{"class":84},[66,2772,210],{"class":80},[14,2774,2775],{},"This works, but if out-of-range access is a normal possibility, checking first is often easier to read.",[52,2777,2779],{"id":2778},"see-also","See also",[25,2781,2782,2786,2794,2800,2806,2811,2817,2823],{},[28,2783,2784],{},[44,2785,390],{"href":389},[28,2787,2788],{},[44,2789,2790,2791,2793],{"href":46},"Python ",[17,2792,49],{}," explained",[28,2795,2796],{},[44,2797,2790,2798,784],{"href":780},[17,2799,654],{},[28,2801,2802],{},[44,2803,2790,2804,784],{"href":2085},[17,2805,2088],{},[28,2807,2808],{},[44,2809,2810],{"href":1226},"How to loop through a list in Python",[28,2812,2813],{},[44,2814,2816],{"href":2815},"\u002Fhow-to\u002Fhow-to-find-an-item-in-a-list-in-python\u002F","How to find an item in a list in Python",[28,2818,2819],{},[44,2820,2822],{"href":2821},"\u002Ferrors\u002Findexerror-in-python-causes-and-fixes\u002F","IndexError in Python: causes and fixes",[28,2824,2825],{},[44,2826,2828],{"href":2827},"\u002Ferrors\u002Findexerror-tuple-index-out-of-range-fix\u002F","IndexError: tuple index out of range",[2830,2831,2832],"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 .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 .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 .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":62,"searchDepth":116,"depth":116,"links":2834},[2835,2836,2839,2842,2850,2859,2866,2873,2880,2884,2890],{"id":54,"depth":116,"text":55},{"id":259,"depth":116,"text":260,"children":2837},[2838],{"id":347,"depth":128,"text":348},{"id":394,"depth":116,"text":395,"children":2840},[2841],{"id":478,"depth":128,"text":479},{"id":518,"depth":116,"text":519,"children":2843},[2844,2845,2846,2848,2849],{"id":525,"depth":128,"text":526},{"id":580,"depth":128,"text":581},{"id":650,"depth":128,"text":2847},"Looping with the wrong range() stop value",{"id":787,"depth":128,"text":788},{"id":822,"depth":128,"text":823},{"id":903,"depth":116,"text":904,"children":2851},[2852,2854,2855,2856,2858],{"id":910,"depth":128,"text":2853},"1. Check the list length with len() before indexing",{"id":1025,"depth":128,"text":1026},{"id":1148,"depth":128,"text":1149},{"id":1230,"depth":128,"text":2857},"4. Check for an empty list before using my_list[0]",{"id":1308,"depth":128,"text":1309},{"id":1493,"depth":116,"text":1494,"children":2860},[2861,2862,2864,2865],{"id":1500,"depth":128,"text":1501},{"id":1567,"depth":128,"text":2863},"Using i + 1 near the end of a loop",{"id":1678,"depth":128,"text":1679},{"id":1775,"depth":128,"text":1776},{"id":1861,"depth":116,"text":1862,"children":2867},[2868,2869,2870,2871,2872],{"id":1868,"depth":128,"text":1869},{"id":1891,"depth":128,"text":1892},{"id":1920,"depth":128,"text":1921},{"id":1942,"depth":128,"text":1943},{"id":1982,"depth":128,"text":1983},{"id":2092,"depth":116,"text":2093,"children":2874},[2875,2876,2878,2879],{"id":2099,"depth":128,"text":2100},{"id":2168,"depth":128,"text":2877},"Use enumerate() when you need both index and value",{"id":2282,"depth":128,"text":2283},{"id":2354,"depth":128,"text":2355},{"id":2461,"depth":116,"text":2462,"children":2881},[2882],{"id":2499,"depth":128,"text":2883},"Why my_list[len(my_list)] is wrong",{"id":2591,"depth":116,"text":2592,"children":2885},[2886,2887,2888,2889],{"id":2595,"depth":128,"text":2596},{"id":2611,"depth":128,"text":2612},{"id":2624,"depth":128,"text":2625},{"id":2670,"depth":128,"text":2671},{"id":2778,"depth":116,"text":2779},"Master indexerror list index out of range fix explained in our comprehensive Python beginner guide.","md",{},"\u002Ferrors\u002Findexerror-list-index-out-of-range-fix-explained",{"title":5,"description":2891},"errors\u002Findexerror-list-index-out-of-range-fix-explained","7-22twfR2SS-kHSxrQFurivHgUDk4xVC_dPiPBfOavA",1777585505429]