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