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