[{"data":1,"prerenderedAt":1446},["ShallowReactive",2],{"doc-\u002Fhow-to\u002Fhow-to-get-the-length-of-a-list-in-python":3},{"id":4,"title":5,"body":6,"description":1439,"extension":1440,"meta":1441,"navigation":284,"path":1442,"seo":1443,"stem":1444,"__hash__":1445},"content\u002Fhow-to\u002Fhow-to-get-the-length-of-a-list-in-python.md","How to Get the Length of a List in Python",{"type":7,"value":8,"toc":1407},"minimark",[9,13,22,33,38,127,134,138,160,166,172,217,220,229,232,248,251,311,324,328,331,405,407,416,419,439,443,449,479,481,490,496,512,514,522,525,529,535,540,620,624,701,704,717,725,729,732,743,746,791,794,833,838,842,851,932,937,943,946,1002,1019,1022,1077,1084,1091,1094,1195,1198,1271,1274,1285,1291,1298,1301,1307,1324,1329,1333,1337,1343,1350,1355,1359,1364,1371,1374,1378,1403],[10,11,5],"h1",{"id":12},"how-to-get-the-length-of-a-list-in-python",[14,15,16,17,21],"p",{},"If you want to count how many items are in a Python list, use ",[18,19,20],"code",{},"len()",".",[14,23,24,25,27,28,21],{},"This page shows the quickest way to do that, explains what ",[18,26,20],{}," returns, and points out common beginner mistakes. If you want a broader explanation of the function, see ",[29,30,32],"a",{"href":31},"\u002Freference\u002Fpython-len-function-explained\u002F","Python len() Function Explained",[34,35,37],"h2",{"id":36},"quick-answer","Quick answer",[39,40,45],"pre",{"className":41,"code":42,"language":43,"meta":44,"style":44},"language-python shiki shiki-themes material-theme-lighter github-light github-dark","numbers = [10, 20, 30, 40]\ncount = len(numbers)\nprint(count)  # 4\n","python","",[18,46,47,87,109],{"__ignoreMap":44},[48,49,52,56,60,64,68,71,74,76,79,81,84],"span",{"class":50,"line":51},"line",1,[48,53,55],{"class":54},"su5hD","numbers ",[48,57,59],{"class":58},"smGrS","=",[48,61,63],{"class":62},"sP7_E"," [",[48,65,67],{"class":66},"srdBf","10",[48,69,70],{"class":62},",",[48,72,73],{"class":66}," 20",[48,75,70],{"class":62},[48,77,78],{"class":66}," 30",[48,80,70],{"class":62},[48,82,83],{"class":66}," 40",[48,85,86],{"class":62},"]\n",[48,88,90,93,95,99,102,106],{"class":50,"line":89},2,[48,91,92],{"class":54},"count ",[48,94,59],{"class":58},[48,96,98],{"class":97},"sptTA"," len",[48,100,101],{"class":62},"(",[48,103,105],{"class":104},"slqww","numbers",[48,107,108],{"class":62},")\n",[48,110,112,115,117,120,123],{"class":50,"line":111},3,[48,113,114],{"class":97},"print",[48,116,101],{"class":62},[48,118,119],{"class":104},"count",[48,121,122],{"class":62},")",[48,124,126],{"class":125},"sutJx","  # 4\n",[14,128,129,130,133],{},"Use ",[18,131,132],{},"len(list_name)"," to get the number of items in a list.",[34,135,137],{"id":136},"what-this-page-helps-you-do","What this page helps you do",[139,140,141,145,151,157],"ul",{},[142,143,144],"li",{},"Get the number of items in a list",[142,146,147,148,150],{},"Use the built-in ",[18,149,20],{}," function",[142,152,153,154,156],{},"Understand what ",[18,155,20],{}," returns",[142,158,159],{},"Avoid common beginner mistakes",[34,161,129,163,165],{"id":162},"use-len-to-count-list-items",[18,164,20],{}," to count list items",[14,167,168,169,171],{},"To get the length of a list, pass the list into ",[18,170,20],{},":",[39,173,175],{"className":41,"code":174,"language":43,"meta":44,"style":44},"numbers = [10, 20, 30, 40]\nprint(len(numbers))\n",[18,176,177,201],{"__ignoreMap":44},[48,178,179,181,183,185,187,189,191,193,195,197,199],{"class":50,"line":51},[48,180,55],{"class":54},[48,182,59],{"class":58},[48,184,63],{"class":62},[48,186,67],{"class":66},[48,188,70],{"class":62},[48,190,73],{"class":66},[48,192,70],{"class":62},[48,194,78],{"class":66},[48,196,70],{"class":62},[48,198,83],{"class":66},[48,200,86],{"class":62},[48,202,203,205,207,210,212,214],{"class":50,"line":89},[48,204,114],{"class":97},[48,206,101],{"class":62},[48,208,209],{"class":97},"len",[48,211,101],{"class":62},[48,213,105],{"class":104},[48,215,216],{"class":62},"))\n",[14,218,219],{},"Output:",[39,221,223],{"className":41,"code":222,"language":43,"meta":44,"style":44},"4\n",[18,224,225],{"__ignoreMap":44},[48,226,227],{"class":50,"line":51},[48,228,222],{"class":66},[14,230,231],{},"What happens here:",[139,233,234,239,245],{},[142,235,236,238],{},[18,237,105],{}," is a list with 4 elements",[142,240,241,244],{},[18,242,243],{},"len(numbers)"," returns an integer",[142,246,247],{},"That integer is the number of items in the list",[14,249,250],{},"You can print the result directly or store it in a variable:",[39,252,254],{"className":41,"code":253,"language":43,"meta":44,"style":44},"numbers = [10, 20, 30, 40]\n\ncount = len(numbers)\nprint(count)\n",[18,255,256,280,286,300],{"__ignoreMap":44},[48,257,258,260,262,264,266,268,270,272,274,276,278],{"class":50,"line":51},[48,259,55],{"class":54},[48,261,59],{"class":58},[48,263,63],{"class":62},[48,265,67],{"class":66},[48,267,70],{"class":62},[48,269,73],{"class":66},[48,271,70],{"class":62},[48,273,78],{"class":66},[48,275,70],{"class":62},[48,277,83],{"class":66},[48,279,86],{"class":62},[48,281,282],{"class":50,"line":89},[48,283,285],{"emptyLinePlaceholder":284},true,"\n",[48,287,288,290,292,294,296,298],{"class":50,"line":111},[48,289,92],{"class":54},[48,291,59],{"class":58},[48,293,98],{"class":97},[48,295,101],{"class":62},[48,297,105],{"class":104},[48,299,108],{"class":62},[48,301,303,305,307,309],{"class":50,"line":302},4,[48,304,114],{"class":97},[48,306,101],{"class":62},[48,308,119],{"class":104},[48,310,108],{"class":62},[14,312,313,314,316,317,321,322,21],{},"If you want more detail about how ",[18,315,20],{}," works with different Python objects, read ",[29,318,320],{"href":319},"\u002Freference\u002Fpython-list-length-len\u002F","Python list length with len()"," or ",[29,323,32],{"href":31},[34,325,327],{"id":326},"example-with-a-list-of-strings","Example with a list of strings",[14,329,330],{},"Each item in a list counts as one element.",[39,332,334],{"className":41,"code":333,"language":43,"meta":44,"style":44},"fruits = [\"apple\", \"banana\", \"orange\"]\ncount = len(fruits)\n\nprint(count)\n",[18,335,336,376,391,395],{"__ignoreMap":44},[48,337,338,341,343,345,349,353,355,357,360,363,365,367,369,372,374],{"class":50,"line":51},[48,339,340],{"class":54},"fruits ",[48,342,59],{"class":58},[48,344,63],{"class":62},[48,346,348],{"class":347},"sjJ54","\"",[48,350,352],{"class":351},"s_sjI","apple",[48,354,348],{"class":347},[48,356,70],{"class":62},[48,358,359],{"class":347}," \"",[48,361,362],{"class":351},"banana",[48,364,348],{"class":347},[48,366,70],{"class":62},[48,368,359],{"class":347},[48,370,371],{"class":351},"orange",[48,373,348],{"class":347},[48,375,86],{"class":62},[48,377,378,380,382,384,386,389],{"class":50,"line":89},[48,379,92],{"class":54},[48,381,59],{"class":58},[48,383,98],{"class":97},[48,385,101],{"class":62},[48,387,388],{"class":104},"fruits",[48,390,108],{"class":62},[48,392,393],{"class":50,"line":111},[48,394,285],{"emptyLinePlaceholder":284},[48,396,397,399,401,403],{"class":50,"line":302},[48,398,114],{"class":97},[48,400,101],{"class":62},[48,402,119],{"class":104},[48,404,108],{"class":62},[14,406,219],{},[39,408,410],{"className":41,"code":409,"language":43,"meta":44,"style":44},"3\n",[18,411,412],{"__ignoreMap":44},[48,413,414],{"class":50,"line":51},[48,415,409],{"class":66},[14,417,418],{},"Explanation:",[139,420,421,424,433,436],{},[142,422,423],{},"The list has 3 string items",[142,425,426,429,430],{},[18,427,428],{},"len(fruits)"," returns ",[18,431,432],{},"3",[142,434,435],{},"It does not matter how long each word is",[142,437,438],{},"Each list item counts as one item",[34,440,442],{"id":441},"empty-lists","Empty lists",[14,444,445,446,21],{},"An empty list has length ",[18,447,448],{},"0",[39,450,452],{"className":41,"code":451,"language":43,"meta":44,"style":44},"items = []\nprint(len(items))\n",[18,453,454,464],{"__ignoreMap":44},[48,455,456,459,461],{"class":50,"line":51},[48,457,458],{"class":54},"items ",[48,460,59],{"class":58},[48,462,463],{"class":62}," []\n",[48,465,466,468,470,472,474,477],{"class":50,"line":89},[48,467,114],{"class":97},[48,469,101],{"class":62},[48,471,209],{"class":97},[48,473,101],{"class":62},[48,475,476],{"class":104},"items",[48,478,216],{"class":62},[14,480,219],{},[39,482,484],{"className":41,"code":483,"language":43,"meta":44,"style":44},"0\n",[18,485,486],{"__ignoreMap":44},[48,487,488],{"class":50,"line":51},[48,489,483],{"class":66},[14,491,492,493,495],{},"You can also call ",[18,494,20],{}," directly on an empty list:",[39,497,499],{"className":41,"code":498,"language":43,"meta":44,"style":44},"print(len([]))\n",[18,500,501],{"__ignoreMap":44},[48,502,503,505,507,509],{"class":50,"line":51},[48,504,114],{"class":97},[48,506,101],{"class":62},[48,508,209],{"class":97},[48,510,511],{"class":62},"([]))\n",[14,513,219],{},[39,515,516],{"className":41,"code":483,"language":43,"meta":44,"style":44},[18,517,518],{"__ignoreMap":44},[48,519,520],{"class":50,"line":51},[48,521,483],{"class":66},[14,523,524],{},"This is useful when checking whether a list has items before you try to access them or loop through them.",[34,526,528],{"id":527},"length-after-changing-a-list","Length after changing a list",[14,530,531,532,534],{},"If the list changes, the length changes too. Call ",[18,533,20],{}," again to get the current length.",[536,537,539],"h3",{"id":538},"after-adding-an-item","After adding an item",[39,541,543],{"className":41,"code":542,"language":43,"meta":44,"style":44},"numbers = [10, 20, 30]\nprint(len(numbers))  # 3\n\nnumbers.append(40)\nprint(len(numbers))  # 4\n",[18,544,545,565,583,587,603],{"__ignoreMap":44},[48,546,547,549,551,553,555,557,559,561,563],{"class":50,"line":51},[48,548,55],{"class":54},[48,550,59],{"class":58},[48,552,63],{"class":62},[48,554,67],{"class":66},[48,556,70],{"class":62},[48,558,73],{"class":66},[48,560,70],{"class":62},[48,562,78],{"class":66},[48,564,86],{"class":62},[48,566,567,569,571,573,575,577,580],{"class":50,"line":89},[48,568,114],{"class":97},[48,570,101],{"class":62},[48,572,209],{"class":97},[48,574,101],{"class":62},[48,576,105],{"class":104},[48,578,579],{"class":62},"))",[48,581,582],{"class":125},"  # 3\n",[48,584,585],{"class":50,"line":111},[48,586,285],{"emptyLinePlaceholder":284},[48,588,589,591,593,596,598,601],{"class":50,"line":302},[48,590,105],{"class":54},[48,592,21],{"class":62},[48,594,595],{"class":104},"append",[48,597,101],{"class":62},[48,599,600],{"class":66},"40",[48,602,108],{"class":62},[48,604,606,608,610,612,614,616,618],{"class":50,"line":605},5,[48,607,114],{"class":97},[48,609,101],{"class":62},[48,611,209],{"class":97},[48,613,101],{"class":62},[48,615,105],{"class":104},[48,617,579],{"class":62},[48,619,126],{"class":125},[536,621,623],{"id":622},"after-removing-an-item","After removing an item",[39,625,627],{"className":41,"code":626,"language":43,"meta":44,"style":44},"numbers = [10, 20, 30, 40]\nprint(len(numbers))  # 4\n\nnumbers.pop()\nprint(len(numbers))  # 3\n",[18,628,629,653,669,673,685],{"__ignoreMap":44},[48,630,631,633,635,637,639,641,643,645,647,649,651],{"class":50,"line":51},[48,632,55],{"class":54},[48,634,59],{"class":58},[48,636,63],{"class":62},[48,638,67],{"class":66},[48,640,70],{"class":62},[48,642,73],{"class":66},[48,644,70],{"class":62},[48,646,78],{"class":66},[48,648,70],{"class":62},[48,650,83],{"class":66},[48,652,86],{"class":62},[48,654,655,657,659,661,663,665,667],{"class":50,"line":89},[48,656,114],{"class":97},[48,658,101],{"class":62},[48,660,209],{"class":97},[48,662,101],{"class":62},[48,664,105],{"class":104},[48,666,579],{"class":62},[48,668,126],{"class":125},[48,670,671],{"class":50,"line":111},[48,672,285],{"emptyLinePlaceholder":284},[48,674,675,677,679,682],{"class":50,"line":302},[48,676,105],{"class":54},[48,678,21],{"class":62},[48,680,681],{"class":104},"pop",[48,683,684],{"class":62},"()\n",[48,686,687,689,691,693,695,697,699],{"class":50,"line":605},[48,688,114],{"class":97},[48,690,101],{"class":62},[48,692,209],{"class":97},[48,694,101],{"class":62},[48,696,105],{"class":104},[48,698,579],{"class":62},[48,700,582],{"class":125},[14,702,703],{},"So:",[139,705,706,709,712],{},[142,707,708],{},"Adding an item increases the length",[142,710,711],{},"Removing an item decreases the length",[142,713,714,716],{},[18,715,20],{}," gives the current number of items, not an old saved value",[14,718,719,720,724],{},"If you are working with lists often, ",[29,721,723],{"href":722},"\u002Fhow-to\u002Fhow-to-loop-through-a-list-in-python\u002F","how to loop through a list in Python"," is a helpful next step.",[34,726,728],{"id":727},"common-mistakes","Common mistakes",[14,730,731],{},"Here are some common problems beginners run into.",[536,733,735,736,739,740],{"id":734},"using-my_listlen-instead-of-lenmy_list","Using ",[18,737,738],{},"my_list.len()"," instead of ",[18,741,742],{},"len(my_list)",[14,744,745],{},"This is wrong:",[39,747,749],{"className":41,"code":748,"language":43,"meta":44,"style":44},"my_list = [1, 2, 3]\nprint(my_list.len())\n",[18,750,751,775],{"__ignoreMap":44},[48,752,753,756,758,760,763,765,768,770,773],{"class":50,"line":51},[48,754,755],{"class":54},"my_list ",[48,757,59],{"class":58},[48,759,63],{"class":62},[48,761,762],{"class":66},"1",[48,764,70],{"class":62},[48,766,767],{"class":66}," 2",[48,769,70],{"class":62},[48,771,772],{"class":66}," 3",[48,774,86],{"class":62},[48,776,777,779,781,784,786,788],{"class":50,"line":89},[48,778,114],{"class":97},[48,780,101],{"class":62},[48,782,783],{"class":104},"my_list",[48,785,21],{"class":62},[48,787,209],{"class":104},[48,789,790],{"class":62},"())\n",[14,792,793],{},"Use this instead:",[39,795,797],{"className":41,"code":796,"language":43,"meta":44,"style":44},"my_list = [1, 2, 3]\nprint(len(my_list))\n",[18,798,799,819],{"__ignoreMap":44},[48,800,801,803,805,807,809,811,813,815,817],{"class":50,"line":51},[48,802,755],{"class":54},[48,804,59],{"class":58},[48,806,63],{"class":62},[48,808,762],{"class":66},[48,810,70],{"class":62},[48,812,767],{"class":66},[48,814,70],{"class":62},[48,816,772],{"class":66},[48,818,86],{"class":62},[48,820,821,823,825,827,829,831],{"class":50,"line":89},[48,822,114],{"class":97},[48,824,101],{"class":62},[48,826,209],{"class":97},[48,828,101],{"class":62},[48,830,783],{"class":104},[48,832,216],{"class":62},[14,834,835,837],{},[18,836,20],{}," is a built-in function. It is not a method on the list object.",[536,839,841],{"id":840},"confusing-the-last-index-with-the-length","Confusing the last index with the length",[14,843,844,845,847,848,21],{},"If a list has 3 items, its length is ",[18,846,432],{},", but the last index is ",[18,849,850],{},"2",[39,852,854],{"className":41,"code":853,"language":43,"meta":44,"style":44},"letters = [\"a\", \"b\", \"c\"]\n\nprint(len(letters))   # 3\nprint(letters[2])     # c\n",[18,855,856,891,895,913],{"__ignoreMap":44},[48,857,858,861,863,865,867,869,871,873,875,878,880,882,884,887,889],{"class":50,"line":51},[48,859,860],{"class":54},"letters ",[48,862,59],{"class":58},[48,864,63],{"class":62},[48,866,348],{"class":347},[48,868,29],{"class":351},[48,870,348],{"class":347},[48,872,70],{"class":62},[48,874,359],{"class":347},[48,876,877],{"class":351},"b",[48,879,348],{"class":347},[48,881,70],{"class":62},[48,883,359],{"class":347},[48,885,886],{"class":351},"c",[48,888,348],{"class":347},[48,890,86],{"class":62},[48,892,893],{"class":50,"line":89},[48,894,285],{"emptyLinePlaceholder":284},[48,896,897,899,901,903,905,908,910],{"class":50,"line":111},[48,898,114],{"class":97},[48,900,101],{"class":62},[48,902,209],{"class":97},[48,904,101],{"class":62},[48,906,907],{"class":104},"letters",[48,909,579],{"class":62},[48,911,912],{"class":125},"   # 3\n",[48,914,915,917,919,921,924,926,929],{"class":50,"line":302},[48,916,114],{"class":97},[48,918,101],{"class":62},[48,920,907],{"class":104},[48,922,923],{"class":62},"[",[48,925,850],{"class":66},[48,927,928],{"class":62},"])",[48,930,931],{"class":125},"     # c\n",[14,933,934,935,21],{},"Why? Because list indexing starts at ",[18,936,448],{},[536,938,735,940,942],{"id":939},"using-lenmy_list-as-an-index",[18,941,742],{}," as an index",[14,944,945],{},"This causes an error:",[39,947,949],{"className":41,"code":948,"language":43,"meta":44,"style":44},"letters = [\"a\", \"b\", \"c\"]\nprint(letters[len(letters)])\n",[18,950,951,983],{"__ignoreMap":44},[48,952,953,955,957,959,961,963,965,967,969,971,973,975,977,979,981],{"class":50,"line":51},[48,954,860],{"class":54},[48,956,59],{"class":58},[48,958,63],{"class":62},[48,960,348],{"class":347},[48,962,29],{"class":351},[48,964,348],{"class":347},[48,966,70],{"class":62},[48,968,359],{"class":347},[48,970,877],{"class":351},[48,972,348],{"class":347},[48,974,70],{"class":62},[48,976,359],{"class":347},[48,978,886],{"class":351},[48,980,348],{"class":347},[48,982,86],{"class":62},[48,984,985,987,989,991,993,995,997,999],{"class":50,"line":89},[48,986,114],{"class":97},[48,988,101],{"class":62},[48,990,907],{"class":104},[48,992,923],{"class":62},[48,994,209],{"class":97},[48,996,101],{"class":62},[48,998,907],{"class":104},[48,1000,1001],{"class":62},")])\n",[14,1003,1004,1007,1008,1010,1011,1013,1014,1016,1017,21],{},[18,1005,1006],{},"len(letters)"," is ",[18,1009,432],{},", but valid indexes are ",[18,1012,448],{},", ",[18,1015,762],{},", and ",[18,1018,850],{},[14,1020,1021],{},"If you want the last item, use:",[39,1023,1025],{"className":41,"code":1024,"language":43,"meta":44,"style":44},"letters = [\"a\", \"b\", \"c\"]\nprint(letters[-1])\n",[18,1026,1027,1059],{"__ignoreMap":44},[48,1028,1029,1031,1033,1035,1037,1039,1041,1043,1045,1047,1049,1051,1053,1055,1057],{"class":50,"line":51},[48,1030,860],{"class":54},[48,1032,59],{"class":58},[48,1034,63],{"class":62},[48,1036,348],{"class":347},[48,1038,29],{"class":351},[48,1040,348],{"class":347},[48,1042,70],{"class":62},[48,1044,359],{"class":347},[48,1046,877],{"class":351},[48,1048,348],{"class":347},[48,1050,70],{"class":62},[48,1052,359],{"class":347},[48,1054,886],{"class":351},[48,1056,348],{"class":347},[48,1058,86],{"class":62},[48,1060,1061,1063,1065,1067,1069,1072,1074],{"class":50,"line":89},[48,1062,114],{"class":97},[48,1064,101],{"class":62},[48,1066,907],{"class":104},[48,1068,923],{"class":62},[48,1070,1071],{"class":58},"-",[48,1073,762],{"class":66},[48,1075,1076],{"class":62},"])\n",[14,1078,1079,1080,21],{},"If you get an indexing problem, see ",[29,1081,1083],{"href":1082},"\u002Ferrors\u002Findexerror-list-index-out-of-range-fix-explained\u002F","IndexError: list index out of range fix explained",[536,1085,1087,1088,1090],{"id":1086},"calling-len-on-a-variable-that-is-not-a-list","Calling ",[18,1089,20],{}," on a variable that is not a list",[14,1092,1093],{},"Check what the variable actually contains:",[39,1095,1097],{"className":41,"code":1096,"language":43,"meta":44,"style":44},"my_list = [1, 2, 3]\n\nprint(my_list)\nprint(type(my_list))\nprint(len(my_list))\nprint(my_list[0])\nprint(my_list[-1])\n",[18,1098,1099,1119,1123,1133,1149,1163,1178],{"__ignoreMap":44},[48,1100,1101,1103,1105,1107,1109,1111,1113,1115,1117],{"class":50,"line":51},[48,1102,755],{"class":54},[48,1104,59],{"class":58},[48,1106,63],{"class":62},[48,1108,762],{"class":66},[48,1110,70],{"class":62},[48,1112,767],{"class":66},[48,1114,70],{"class":62},[48,1116,772],{"class":66},[48,1118,86],{"class":62},[48,1120,1121],{"class":50,"line":89},[48,1122,285],{"emptyLinePlaceholder":284},[48,1124,1125,1127,1129,1131],{"class":50,"line":111},[48,1126,114],{"class":97},[48,1128,101],{"class":62},[48,1130,783],{"class":104},[48,1132,108],{"class":62},[48,1134,1135,1137,1139,1143,1145,1147],{"class":50,"line":302},[48,1136,114],{"class":97},[48,1138,101],{"class":62},[48,1140,1142],{"class":1141},"sZMiF","type",[48,1144,101],{"class":62},[48,1146,783],{"class":104},[48,1148,216],{"class":62},[48,1150,1151,1153,1155,1157,1159,1161],{"class":50,"line":605},[48,1152,114],{"class":97},[48,1154,101],{"class":62},[48,1156,209],{"class":97},[48,1158,101],{"class":62},[48,1160,783],{"class":104},[48,1162,216],{"class":62},[48,1164,1166,1168,1170,1172,1174,1176],{"class":50,"line":1165},6,[48,1167,114],{"class":97},[48,1169,101],{"class":62},[48,1171,783],{"class":104},[48,1173,923],{"class":62},[48,1175,448],{"class":66},[48,1177,1076],{"class":62},[48,1179,1181,1183,1185,1187,1189,1191,1193],{"class":50,"line":1180},7,[48,1182,114],{"class":97},[48,1184,101],{"class":62},[48,1186,783],{"class":104},[48,1188,923],{"class":62},[48,1190,1071],{"class":58},[48,1192,762],{"class":66},[48,1194,1076],{"class":62},[14,1196,1197],{},"Useful debugging checks:",[39,1199,1201],{"className":41,"code":1200,"language":43,"meta":44,"style":44},"print(my_list)\nprint(type(my_list))\nprint(len(my_list))\nprint(my_list[0])\nprint(my_list[-1])\n",[18,1202,1203,1213,1227,1241,1255],{"__ignoreMap":44},[48,1204,1205,1207,1209,1211],{"class":50,"line":51},[48,1206,114],{"class":97},[48,1208,101],{"class":62},[48,1210,783],{"class":104},[48,1212,108],{"class":62},[48,1214,1215,1217,1219,1221,1223,1225],{"class":50,"line":89},[48,1216,114],{"class":97},[48,1218,101],{"class":62},[48,1220,1142],{"class":1141},[48,1222,101],{"class":62},[48,1224,783],{"class":104},[48,1226,216],{"class":62},[48,1228,1229,1231,1233,1235,1237,1239],{"class":50,"line":111},[48,1230,114],{"class":97},[48,1232,101],{"class":62},[48,1234,209],{"class":97},[48,1236,101],{"class":62},[48,1238,783],{"class":104},[48,1240,216],{"class":62},[48,1242,1243,1245,1247,1249,1251,1253],{"class":50,"line":302},[48,1244,114],{"class":97},[48,1246,101],{"class":62},[48,1248,783],{"class":104},[48,1250,923],{"class":62},[48,1252,448],{"class":66},[48,1254,1076],{"class":62},[48,1256,1257,1259,1261,1263,1265,1267,1269],{"class":50,"line":605},[48,1258,114],{"class":97},[48,1260,101],{"class":62},[48,1262,783],{"class":104},[48,1264,923],{"class":62},[48,1266,1071],{"class":58},[48,1268,762],{"class":66},[48,1270,1076],{"class":62},[14,1272,1273],{},"These help you confirm:",[139,1275,1276,1279,1282],{},[142,1277,1278],{},"the variable is really a list",[142,1280,1281],{},"the list has the number of items you expect",[142,1283,1284],{},"the first and last items are what you think they are",[14,1286,1287,1288,1290],{},"Also remember that ",[18,1289,20],{}," works on many container types, not just lists.",[34,1292,1294,1295,1297],{"id":1293},"when-to-use-this-vs-the-len-reference-page","When to use this vs the ",[18,1296,20],{}," reference page",[14,1299,1300],{},"Use this page when you want to solve one task quickly: get the number of items in a list.",[14,1302,1303,1304,1306],{},"Use the reference page when you want to understand ",[18,1305,20],{}," more generally, including how it works with:",[139,1308,1309,1312,1315,1318,1321],{},[142,1310,1311],{},"strings",[142,1313,1314],{},"tuples",[142,1316,1317],{},"dictionaries",[142,1319,1320],{},"sets",[142,1322,1323],{},"other Python objects",[14,1325,1326,1327,21],{},"For that, read ",[29,1328,32],{"href":31},[34,1330,1332],{"id":1331},"faq","FAQ",[536,1334,1336],{"id":1335},"how-do-i-count-items-in-a-list-in-python","How do I count items in a list in Python?",[14,1338,129,1339,1342],{},[18,1340,1341],{},"len(your_list)",". It returns the number of items in the list.",[536,1344,1346,1347,1349],{"id":1345},"does-len-work-on-an-empty-list","Does ",[18,1348,20],{}," work on an empty list?",[14,1351,1352,1353,21],{},"Yes. It returns ",[18,1354,448],{},[536,1356,1358],{"id":1357},"is-the-length-the-same-as-the-last-index","Is the length the same as the last index?",[14,1360,1361,1362,21],{},"No. The length is the number of items. The last index is length minus ",[18,1363,762],{},[536,1365,1367,1368,1370],{"id":1366},"can-i-use-len-on-things-other-than-lists","Can I use ",[18,1369,20],{}," on things other than lists?",[14,1372,1373],{},"Yes. It also works with strings, tuples, dictionaries, sets, and more.",[34,1375,1377],{"id":1376},"see-also","See also",[139,1379,1380,1384,1388,1393,1399],{},[142,1381,1382],{},[29,1383,320],{"href":319},[142,1385,1386],{},[29,1387,32],{"href":31},[142,1389,1390],{},[29,1391,1392],{"href":722},"How to loop through a list in Python",[142,1394,1395],{},[29,1396,1398],{"href":1397},"\u002Fhow-to\u002Fhow-to-find-an-item-in-a-list-in-python\u002F","How to find an item in a list in Python",[142,1400,1401],{},[29,1402,1083],{"href":1082},[1404,1405,1406],"style",{},"html pre.shiki code .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sutJx, html code.shiki .sutJx{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#6A737D;--shiki-default-font-style:inherit;--shiki-dark:#6A737D;--shiki-dark-font-style:inherit}html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .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 .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":44,"searchDepth":89,"depth":89,"links":1408},[1409,1410,1411,1413,1414,1415,1419,1428,1430,1438],{"id":36,"depth":89,"text":37},{"id":136,"depth":89,"text":137},{"id":162,"depth":89,"text":1412},"Use len() to count list items",{"id":326,"depth":89,"text":327},{"id":441,"depth":89,"text":442},{"id":527,"depth":89,"text":528,"children":1416},[1417,1418],{"id":538,"depth":111,"text":539},{"id":622,"depth":111,"text":623},{"id":727,"depth":89,"text":728,"children":1420},[1421,1423,1424,1426],{"id":734,"depth":111,"text":1422},"Using my_list.len() instead of len(my_list)",{"id":840,"depth":111,"text":841},{"id":939,"depth":111,"text":1425},"Using len(my_list) as an index",{"id":1086,"depth":111,"text":1427},"Calling len() on a variable that is not a list",{"id":1293,"depth":89,"text":1429},"When to use this vs the len() reference page",{"id":1331,"depth":89,"text":1332,"children":1431},[1432,1433,1435,1436],{"id":1335,"depth":111,"text":1336},{"id":1345,"depth":111,"text":1434},"Does len() work on an empty list?",{"id":1357,"depth":111,"text":1358},{"id":1366,"depth":111,"text":1437},"Can I use len() on things other than lists?",{"id":1376,"depth":89,"text":1377},"Master how to get the length of a list in python in our comprehensive Python beginner guide.","md",{},"\u002Fhow-to\u002Fhow-to-get-the-length-of-a-list-in-python",{"title":5,"description":1439},"how-to\u002Fhow-to-get-the-length-of-a-list-in-python","VrweE0henZ1-ad5JOk7trjXFrwpE2eH5G7HqAb6bufU",1777585505838]