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