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