[{"data":1,"prerenderedAt":2192},["ShallowReactive",2],{"doc-\u002Ferrors\u002Fkeyerror-when-accessing-dictionary-values-fix":3},{"id":4,"title":5,"body":6,"description":2185,"extension":2186,"meta":2187,"navigation":92,"path":2188,"seo":2189,"stem":2190,"__hash__":2191},"content\u002Ferrors\u002Fkeyerror-when-accessing-dictionary-values-fix.md","KeyError when accessing dictionary values (Fix)",{"type":7,"value":8,"toc":2157},"minimark",[9,13,22,29,32,37,229,240,244,249,252,299,302,322,325,344,352,356,359,380,383,482,487,491,494,547,550,566,569,592,599,603,609,676,678,688,691,763,765,774,777,806,809,816,820,823,910,912,928,931,934,945,952,956,962,1045,1047,1061,1064,1075,1081,1084,1174,1176,1184,1188,1191,1194,1275,1278,1313,1316,1428,1430,1508,1516,1523,1527,1530,1535,1602,1611,1615,1730,1734,1818,1822,1909,1912,1959,1961,1975,1985,1989,1992,2013,2017,2022,2052,2056,2068,2080,2087,2093,2100,2105,2109,2112,2116,2141,2153],[10,11,5],"h1",{"id":12},"keyerror-when-accessing-dictionary-values-fix",[14,15,16,17,21],"p",{},"A ",[18,19,20],"code",{},"KeyError"," happens when you try to access a dictionary value using a key that does not exist.",[14,23,24,25,28],{},"This usually happens with square bracket access like ",[18,26,27],{},"data[\"age\"]",". If the key is missing, Python stops and raises an error.",[14,30,31],{},"This page shows what the error means, why it happens, and how to fix it safely.",[33,34,36],"h2",{"id":35},"quick-fix","Quick fix",[38,39,44],"pre",{"className":40,"code":41,"language":42,"meta":43,"style":43},"language-python shiki shiki-themes material-theme-lighter github-light github-dark","data = {\"name\": \"Ana\"}\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,45,46,87,94,101,132,150,155,161,182,205,213],{"__ignoreMap":43},[47,48,51,55,59,63,67,71,73,76,79,82,84],"span",{"class":49,"line":50},"line",1,[47,52,54],{"class":53},"su5hD","data ",[47,56,58],{"class":57},"smGrS","=",[47,60,62],{"class":61},"sP7_E"," {",[47,64,66],{"class":65},"sjJ54","\"",[47,68,70],{"class":69},"s_sjI","name",[47,72,66],{"class":65},[47,74,75],{"class":61},":",[47,77,78],{"class":65}," \"",[47,80,81],{"class":69},"Ana",[47,83,66],{"class":65},[47,85,86],{"class":61},"}\n",[47,88,90],{"class":49,"line":89},2,[47,91,93],{"emptyLinePlaceholder":92},true,"\n",[47,95,97],{"class":49,"line":96},3,[47,98,100],{"class":99},"sutJx","# Safe access\n",[47,102,104,107,109,112,115,119,122,124,127,129],{"class":49,"line":103},4,[47,105,106],{"class":53},"value ",[47,108,58],{"class":57},[47,110,111],{"class":53}," data",[47,113,114],{"class":61},".",[47,116,118],{"class":117},"slqww","get",[47,120,121],{"class":61},"(",[47,123,66],{"class":65},[47,125,126],{"class":69},"age",[47,128,66],{"class":65},[47,130,131],{"class":61},")\n",[47,133,135,139,141,144,147],{"class":49,"line":134},5,[47,136,138],{"class":137},"sptTA","print",[47,140,121],{"class":61},[47,142,143],{"class":117},"value",[47,145,146],{"class":61},")",[47,148,149],{"class":99},"  # None\n",[47,151,153],{"class":49,"line":152},6,[47,154,93],{"emptyLinePlaceholder":92},[47,156,158],{"class":49,"line":157},7,[47,159,160],{"class":99},"# Or check first\n",[47,162,164,168,170,172,174,177,179],{"class":49,"line":163},8,[47,165,167],{"class":166},"sVHd0","if",[47,169,78],{"class":65},[47,171,126],{"class":69},[47,173,66],{"class":65},[47,175,176],{"class":57}," in",[47,178,111],{"class":53},[47,180,181],{"class":61},":\n",[47,183,185,188,190,193,196,198,200,202],{"class":49,"line":184},9,[47,186,187],{"class":137},"    print",[47,189,121],{"class":61},[47,191,192],{"class":117},"data",[47,194,195],{"class":61},"[",[47,197,66],{"class":65},[47,199,126],{"class":69},[47,201,66],{"class":65},[47,203,204],{"class":61},"])\n",[47,206,208,211],{"class":49,"line":207},10,[47,209,210],{"class":166},"else",[47,212,181],{"class":61},[47,214,216,218,220,222,225,227],{"class":49,"line":215},11,[47,217,187],{"class":137},[47,219,121],{"class":61},[47,221,66],{"class":65},[47,223,224],{"class":69},"Key not found",[47,226,66],{"class":65},[47,228,131],{"class":61},[14,230,231,232,235,236,239],{},"Use ",[18,233,234],{},"dict.get()"," when a key might be missing, or check with ",[18,237,238],{},"in"," before using square brackets.",[33,241,243],{"id":242},"what-this-error-means","What this error means",[14,245,16,246,248],{},[18,247,20],{}," means Python could not find the key in the dictionary.",[14,250,251],{},"For example:",[38,253,255],{"className":40,"code":254,"language":42,"meta":43,"style":43},"data = {\"name\": \"Ana\"}\nprint(data[\"age\"])\n",[18,256,257,281],{"__ignoreMap":43},[47,258,259,261,263,265,267,269,271,273,275,277,279],{"class":49,"line":50},[47,260,54],{"class":53},[47,262,58],{"class":57},[47,264,62],{"class":61},[47,266,66],{"class":65},[47,268,70],{"class":69},[47,270,66],{"class":65},[47,272,75],{"class":61},[47,274,78],{"class":65},[47,276,81],{"class":69},[47,278,66],{"class":65},[47,280,86],{"class":61},[47,282,283,285,287,289,291,293,295,297],{"class":49,"line":89},[47,284,138],{"class":137},[47,286,121],{"class":61},[47,288,192],{"class":117},[47,290,195],{"class":61},[47,292,66],{"class":65},[47,294,126],{"class":69},[47,296,66],{"class":65},[47,298,204],{"class":61},[14,300,301],{},"This raises:",[38,303,305],{"className":40,"code":304,"language":42,"meta":43,"style":43},"KeyError: 'age'\n",[18,306,307],{"__ignoreMap":43},[47,308,309,312,314,317,319],{"class":49,"line":50},[47,310,20],{"class":311},"sZMiF",[47,313,75],{"class":61},[47,315,316],{"class":65}," '",[47,318,126],{"class":69},[47,320,321],{"class":65},"'\n",[14,323,324],{},"Why?",[326,327,328,335,341],"ul",{},[329,330,331,332,114],"li",{},"The dictionary only has the key ",[18,333,334],{},"\"name\"",[329,336,337,338,114],{},"You asked for ",[18,339,340],{},"\"age\"",[329,342,343],{},"Dictionary keys must match exactly.",[14,345,346,347,114],{},"If you are new to dictionaries, see ",[348,349,351],"a",{"href":350},"\u002Flearn\u002Fpython-dictionaries-explained","Python dictionaries explained",[33,353,355],{"id":354},"why-it-happens","Why it happens",[14,357,358],{},"Common reasons include:",[326,360,361,364,367,370],{},[329,362,363],{},"The key does not exist in the dictionary.",[329,365,366],{},"The key name has different spelling or capitalization.",[329,368,369],{},"You expected data from a file, API, or user input, but the key is missing.",[329,371,372,373,376,377,114],{},"You removed the key earlier with ",[18,374,375],{},"pop()"," or ",[18,378,379],{},"del",[14,381,382],{},"A few examples:",[38,384,386],{"className":40,"code":385,"language":42,"meta":43,"style":43},"data = {\"name\": \"Ana\"}\n\nprint(data[\"Name\"])   # different capitalization\nprint(data[\" name\"])  # leading space\nprint(data[\"age\"])    # missing key\n",[18,387,388,412,416,439,461],{"__ignoreMap":43},[47,389,390,392,394,396,398,400,402,404,406,408,410],{"class":49,"line":50},[47,391,54],{"class":53},[47,393,58],{"class":57},[47,395,62],{"class":61},[47,397,66],{"class":65},[47,399,70],{"class":69},[47,401,66],{"class":65},[47,403,75],{"class":61},[47,405,78],{"class":65},[47,407,81],{"class":69},[47,409,66],{"class":65},[47,411,86],{"class":61},[47,413,414],{"class":49,"line":89},[47,415,93],{"emptyLinePlaceholder":92},[47,417,418,420,422,424,426,428,431,433,436],{"class":49,"line":96},[47,419,138],{"class":137},[47,421,121],{"class":61},[47,423,192],{"class":117},[47,425,195],{"class":61},[47,427,66],{"class":65},[47,429,430],{"class":69},"Name",[47,432,66],{"class":65},[47,434,435],{"class":61},"])",[47,437,438],{"class":99},"   # different capitalization\n",[47,440,441,443,445,447,449,451,454,456,458],{"class":49,"line":103},[47,442,138],{"class":137},[47,444,121],{"class":61},[47,446,192],{"class":117},[47,448,195],{"class":61},[47,450,66],{"class":65},[47,452,453],{"class":69}," name",[47,455,66],{"class":65},[47,457,435],{"class":61},[47,459,460],{"class":99},"  # leading space\n",[47,462,463,465,467,469,471,473,475,477,479],{"class":49,"line":134},[47,464,138],{"class":137},[47,466,121],{"class":61},[47,468,192],{"class":117},[47,470,195],{"class":61},[47,472,66],{"class":65},[47,474,126],{"class":69},[47,476,66],{"class":65},[47,478,435],{"class":61},[47,480,481],{"class":99},"    # missing key\n",[14,483,484,485,114],{},"All of these can raise ",[18,486,20],{},[33,488,490],{"id":489},"example-that-causes-the-error","Example that causes the error",[14,492,493],{},"Here is a simple example:",[38,495,497],{"className":40,"code":496,"language":42,"meta":43,"style":43},"person = {\"name\": \"Ana\"}\n\nprint(person[\"age\"])\n",[18,498,499,524,528],{"__ignoreMap":43},[47,500,501,504,506,508,510,512,514,516,518,520,522],{"class":49,"line":50},[47,502,503],{"class":53},"person ",[47,505,58],{"class":57},[47,507,62],{"class":61},[47,509,66],{"class":65},[47,511,70],{"class":69},[47,513,66],{"class":65},[47,515,75],{"class":61},[47,517,78],{"class":65},[47,519,81],{"class":69},[47,521,66],{"class":65},[47,523,86],{"class":61},[47,525,526],{"class":49,"line":89},[47,527,93],{"emptyLinePlaceholder":92},[47,529,530,532,534,537,539,541,543,545],{"class":49,"line":96},[47,531,138],{"class":137},[47,533,121],{"class":61},[47,535,536],{"class":117},"person",[47,538,195],{"class":61},[47,540,66],{"class":65},[47,542,126],{"class":69},[47,544,66],{"class":65},[47,546,204],{"class":61},[14,548,549],{},"Output:",[38,551,552],{"className":40,"code":304,"language":42,"meta":43,"style":43},[18,553,554],{"__ignoreMap":43},[47,555,556,558,560,562,564],{"class":49,"line":50},[47,557,20],{"class":311},[47,559,75],{"class":61},[47,561,316],{"class":65},[47,563,126],{"class":69},[47,565,321],{"class":65},[14,567,568],{},"What happens here:",[326,570,571,578,583],{},[329,572,573,575,576],{},[18,574,536],{}," contains one key: ",[18,577,334],{},[329,579,580,581],{},"The code tries to read ",[18,582,340],{},[329,584,585,586,588,589,591],{},"Python raises ",[18,587,20],{}," because ",[18,590,340],{}," is not present",[14,593,594,595,114],{},"If you want to learn the normal way to read dictionary values, see ",[348,596,598],{"href":597},"\u002Fhow-to\u002Fhow-to-access-values-in-a-dictionary-in-python","how to access values in a dictionary in Python",[33,600,602],{"id":601},"fix-1-use-get-for-optional-keys","Fix 1: Use get() for optional keys",[14,604,231,605,608],{},[18,606,607],{},".get()"," when a key might be missing.",[38,610,612],{"className":40,"code":611,"language":42,"meta":43,"style":43},"person = {\"name\": \"Ana\"}\n\nage = person.get(\"age\")\nprint(age)\n",[18,613,614,638,642,666],{"__ignoreMap":43},[47,615,616,618,620,622,624,626,628,630,632,634,636],{"class":49,"line":50},[47,617,503],{"class":53},[47,619,58],{"class":57},[47,621,62],{"class":61},[47,623,66],{"class":65},[47,625,70],{"class":69},[47,627,66],{"class":65},[47,629,75],{"class":61},[47,631,78],{"class":65},[47,633,81],{"class":69},[47,635,66],{"class":65},[47,637,86],{"class":61},[47,639,640],{"class":49,"line":89},[47,641,93],{"emptyLinePlaceholder":92},[47,643,644,647,649,652,654,656,658,660,662,664],{"class":49,"line":96},[47,645,646],{"class":53},"age ",[47,648,58],{"class":57},[47,650,651],{"class":53}," person",[47,653,114],{"class":61},[47,655,118],{"class":117},[47,657,121],{"class":61},[47,659,66],{"class":65},[47,661,126],{"class":69},[47,663,66],{"class":65},[47,665,131],{"class":61},[47,667,668,670,672,674],{"class":49,"line":103},[47,669,138],{"class":137},[47,671,121],{"class":61},[47,673,126],{"class":117},[47,675,131],{"class":61},[14,677,549],{},[38,679,681],{"className":40,"code":680,"language":42,"meta":43,"style":43},"None\n",[18,682,683],{"__ignoreMap":43},[47,684,685],{"class":49,"line":50},[47,686,680],{"class":687},"s39Yj",[14,689,690],{},"You can also provide a default value:",[38,692,694],{"className":40,"code":693,"language":42,"meta":43,"style":43},"person = {\"name\": \"Ana\"}\n\nage = person.get(\"age\", 0)\nprint(age)\n",[18,695,696,720,724,753],{"__ignoreMap":43},[47,697,698,700,702,704,706,708,710,712,714,716,718],{"class":49,"line":50},[47,699,503],{"class":53},[47,701,58],{"class":57},[47,703,62],{"class":61},[47,705,66],{"class":65},[47,707,70],{"class":69},[47,709,66],{"class":65},[47,711,75],{"class":61},[47,713,78],{"class":65},[47,715,81],{"class":69},[47,717,66],{"class":65},[47,719,86],{"class":61},[47,721,722],{"class":49,"line":89},[47,723,93],{"emptyLinePlaceholder":92},[47,725,726,728,730,732,734,736,738,740,742,744,747,751],{"class":49,"line":96},[47,727,646],{"class":53},[47,729,58],{"class":57},[47,731,651],{"class":53},[47,733,114],{"class":61},[47,735,118],{"class":117},[47,737,121],{"class":61},[47,739,66],{"class":65},[47,741,126],{"class":69},[47,743,66],{"class":65},[47,745,746],{"class":61},",",[47,748,750],{"class":749},"srdBf"," 0",[47,752,131],{"class":61},[47,754,755,757,759,761],{"class":49,"line":103},[47,756,138],{"class":137},[47,758,121],{"class":61},[47,760,126],{"class":117},[47,762,131],{"class":61},[14,764,549],{},[38,766,768],{"className":40,"code":767,"language":42,"meta":43,"style":43},"0\n",[18,769,770],{"__ignoreMap":43},[47,771,772],{"class":49,"line":50},[47,773,767],{"class":749},[14,775,776],{},"Why this helps:",[326,778,779,787,797],{},[329,780,781,784,785],{},[18,782,783],{},"person[\"age\"]"," raises ",[18,786,20],{},[329,788,789,792,793,796],{},[18,790,791],{},"person.get(\"age\")"," returns ",[18,794,795],{},"None"," instead",[329,798,799,792,802,805],{},[18,800,801],{},"person.get(\"age\", 0)",[18,803,804],{},"0"," if the key is missing",[14,807,808],{},"This is useful when missing data is expected.",[14,810,811,812,114],{},"If you want more detail, see the ",[348,813,815],{"href":814},"\u002Freference\u002Fpython-dictionary-get-method","Python dictionary get() method",[33,817,819],{"id":818},"fix-2-check-if-the-key-exists-first","Fix 2: Check if the key exists first",[14,821,822],{},"You can check before reading the value:",[38,824,826],{"className":40,"code":825,"language":42,"meta":43,"style":43},"person = {\"name\": \"Ana\"}\n\nif \"age\" in person:\n    print(person[\"age\"])\nelse:\n    print(\"Key not found\")\n",[18,827,828,852,856,872,890,896],{"__ignoreMap":43},[47,829,830,832,834,836,838,840,842,844,846,848,850],{"class":49,"line":50},[47,831,503],{"class":53},[47,833,58],{"class":57},[47,835,62],{"class":61},[47,837,66],{"class":65},[47,839,70],{"class":69},[47,841,66],{"class":65},[47,843,75],{"class":61},[47,845,78],{"class":65},[47,847,81],{"class":69},[47,849,66],{"class":65},[47,851,86],{"class":61},[47,853,854],{"class":49,"line":89},[47,855,93],{"emptyLinePlaceholder":92},[47,857,858,860,862,864,866,868,870],{"class":49,"line":96},[47,859,167],{"class":166},[47,861,78],{"class":65},[47,863,126],{"class":69},[47,865,66],{"class":65},[47,867,176],{"class":57},[47,869,651],{"class":53},[47,871,181],{"class":61},[47,873,874,876,878,880,882,884,886,888],{"class":49,"line":103},[47,875,187],{"class":137},[47,877,121],{"class":61},[47,879,536],{"class":117},[47,881,195],{"class":61},[47,883,66],{"class":65},[47,885,126],{"class":69},[47,887,66],{"class":65},[47,889,204],{"class":61},[47,891,892,894],{"class":49,"line":134},[47,893,210],{"class":166},[47,895,181],{"class":61},[47,897,898,900,902,904,906,908],{"class":49,"line":152},[47,899,187],{"class":137},[47,901,121],{"class":61},[47,903,66],{"class":65},[47,905,224],{"class":69},[47,907,66],{"class":65},[47,909,131],{"class":61},[14,911,549],{},[38,913,915],{"className":40,"code":914,"language":42,"meta":43,"style":43},"Key not found\n",[18,916,917],{"__ignoreMap":43},[47,918,919,922,925],{"class":49,"line":50},[47,920,921],{"class":53},"Key ",[47,923,924],{"class":57},"not",[47,926,927],{"class":53}," found\n",[14,929,930],{},"This is a clear and beginner-friendly approach.",[14,932,933],{},"Use this when:",[326,935,936,939,942],{},[329,937,938],{},"You want different behavior for missing keys",[329,940,941],{},"You want to print a message",[329,943,944],{},"You want to avoid an error before it happens",[14,946,947,948,114],{},"For a full example, see ",[348,949,951],{"href":950},"\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",[33,953,955],{"id":954},"fix-3-handle-the-error-with-try-except","Fix 3: Handle the error with try-except",[14,957,958,959,75],{},"You can catch the error with ",[18,960,961],{},"try-except",[38,963,965],{"className":40,"code":964,"language":42,"meta":43,"style":43},"person = {\"name\": \"Ana\"}\n\ntry:\n    print(person[\"age\"])\nexcept KeyError:\n    print(\"The key was not found\")\n",[18,966,967,991,995,1002,1020,1030],{"__ignoreMap":43},[47,968,969,971,973,975,977,979,981,983,985,987,989],{"class":49,"line":50},[47,970,503],{"class":53},[47,972,58],{"class":57},[47,974,62],{"class":61},[47,976,66],{"class":65},[47,978,70],{"class":69},[47,980,66],{"class":65},[47,982,75],{"class":61},[47,984,78],{"class":65},[47,986,81],{"class":69},[47,988,66],{"class":65},[47,990,86],{"class":61},[47,992,993],{"class":49,"line":89},[47,994,93],{"emptyLinePlaceholder":92},[47,996,997,1000],{"class":49,"line":96},[47,998,999],{"class":166},"try",[47,1001,181],{"class":61},[47,1003,1004,1006,1008,1010,1012,1014,1016,1018],{"class":49,"line":103},[47,1005,187],{"class":137},[47,1007,121],{"class":61},[47,1009,536],{"class":117},[47,1011,195],{"class":61},[47,1013,66],{"class":65},[47,1015,126],{"class":69},[47,1017,66],{"class":65},[47,1019,204],{"class":61},[47,1021,1022,1025,1028],{"class":49,"line":134},[47,1023,1024],{"class":166},"except",[47,1026,1027],{"class":311}," KeyError",[47,1029,181],{"class":61},[47,1031,1032,1034,1036,1038,1041,1043],{"class":49,"line":152},[47,1033,187],{"class":137},[47,1035,121],{"class":61},[47,1037,66],{"class":65},[47,1039,1040],{"class":69},"The key was not found",[47,1042,66],{"class":65},[47,1044,131],{"class":61},[14,1046,549],{},[38,1048,1050],{"className":40,"code":1049,"language":42,"meta":43,"style":43},"The key was not found\n",[18,1051,1052],{"__ignoreMap":43},[47,1053,1054,1057,1059],{"class":49,"line":50},[47,1055,1056],{"class":53},"The key was ",[47,1058,924],{"class":57},[47,1060,927],{"class":53},[14,1062,1063],{},"This is useful when:",[326,1065,1066,1069,1072],{},[329,1067,1068],{},"The key might be missing",[329,1070,1071],{},"You want to handle the problem without stopping the program",[329,1073,1074],{},"The missing key is part of normal program flow",[14,1076,1077,1078,1080],{},"Keep the ",[18,1079,999],{}," block small. That makes debugging easier.",[14,1082,1083],{},"You can also use a fallback value:",[38,1085,1087],{"className":40,"code":1086,"language":42,"meta":43,"style":43},"person = {\"name\": \"Ana\"}\n\ntry:\n    age = person[\"age\"]\nexcept KeyError:\n    age = 0\n\nprint(age)\n",[18,1088,1089,1113,1117,1123,1143,1151,1160,1164],{"__ignoreMap":43},[47,1090,1091,1093,1095,1097,1099,1101,1103,1105,1107,1109,1111],{"class":49,"line":50},[47,1092,503],{"class":53},[47,1094,58],{"class":57},[47,1096,62],{"class":61},[47,1098,66],{"class":65},[47,1100,70],{"class":69},[47,1102,66],{"class":65},[47,1104,75],{"class":61},[47,1106,78],{"class":65},[47,1108,81],{"class":69},[47,1110,66],{"class":65},[47,1112,86],{"class":61},[47,1114,1115],{"class":49,"line":89},[47,1116,93],{"emptyLinePlaceholder":92},[47,1118,1119,1121],{"class":49,"line":96},[47,1120,999],{"class":166},[47,1122,181],{"class":61},[47,1124,1125,1128,1130,1132,1134,1136,1138,1140],{"class":49,"line":103},[47,1126,1127],{"class":53},"    age ",[47,1129,58],{"class":57},[47,1131,651],{"class":53},[47,1133,195],{"class":61},[47,1135,66],{"class":65},[47,1137,126],{"class":69},[47,1139,66],{"class":65},[47,1141,1142],{"class":61},"]\n",[47,1144,1145,1147,1149],{"class":49,"line":134},[47,1146,1024],{"class":166},[47,1148,1027],{"class":311},[47,1150,181],{"class":61},[47,1152,1153,1155,1157],{"class":49,"line":152},[47,1154,1127],{"class":53},[47,1156,58],{"class":57},[47,1158,1159],{"class":749}," 0\n",[47,1161,1162],{"class":49,"line":157},[47,1163,93],{"emptyLinePlaceholder":92},[47,1165,1166,1168,1170,1172],{"class":49,"line":163},[47,1167,138],{"class":137},[47,1169,121],{"class":61},[47,1171,126],{"class":117},[47,1173,131],{"class":61},[14,1175,549],{},[38,1177,1178],{"className":40,"code":767,"language":42,"meta":43,"style":43},[18,1179,1180],{"__ignoreMap":43},[47,1181,1182],{"class":49,"line":50},[47,1183,767],{"class":749},[33,1185,1187],{"id":1186},"how-to-debug-a-keyerror","How to debug a KeyError",[14,1189,1190],{},"If you are not sure why the error happens, inspect the dictionary first.",[14,1192,1193],{},"Useful debugging commands:",[38,1195,1197],{"className":40,"code":1196,"language":42,"meta":43,"style":43},"print(data)\nprint(data.keys())\nprint(\"age\" in data)\nprint(repr(user_key))\nprint(type(user_key))\n",[18,1198,1199,1209,1225,1243,1260],{"__ignoreMap":43},[47,1200,1201,1203,1205,1207],{"class":49,"line":50},[47,1202,138],{"class":137},[47,1204,121],{"class":61},[47,1206,192],{"class":117},[47,1208,131],{"class":61},[47,1210,1211,1213,1215,1217,1219,1222],{"class":49,"line":89},[47,1212,138],{"class":137},[47,1214,121],{"class":61},[47,1216,192],{"class":117},[47,1218,114],{"class":61},[47,1220,1221],{"class":117},"keys",[47,1223,1224],{"class":61},"())\n",[47,1226,1227,1229,1231,1233,1235,1237,1239,1241],{"class":49,"line":96},[47,1228,138],{"class":137},[47,1230,121],{"class":61},[47,1232,66],{"class":65},[47,1234,126],{"class":69},[47,1236,66],{"class":65},[47,1238,176],{"class":166},[47,1240,111],{"class":117},[47,1242,131],{"class":61},[47,1244,1245,1247,1249,1252,1254,1257],{"class":49,"line":103},[47,1246,138],{"class":137},[47,1248,121],{"class":61},[47,1250,1251],{"class":137},"repr",[47,1253,121],{"class":61},[47,1255,1256],{"class":117},"user_key",[47,1258,1259],{"class":61},"))\n",[47,1261,1262,1264,1266,1269,1271,1273],{"class":49,"line":134},[47,1263,138],{"class":137},[47,1265,121],{"class":61},[47,1267,1268],{"class":311},"type",[47,1270,121],{"class":61},[47,1272,1256],{"class":117},[47,1274,1259],{"class":61},[14,1276,1277],{},"What these help you check:",[326,1279,1280,1286,1292,1298,1307],{},[329,1281,1282,1285],{},[18,1283,1284],{},"print(data)"," shows the full dictionary",[329,1287,1288,1291],{},[18,1289,1290],{},"print(data.keys())"," shows the available keys",[329,1293,1294,1297],{},[18,1295,1296],{},"print(\"age\" in data)"," checks whether a specific key exists",[329,1299,1300,1303,1304],{},[18,1301,1302],{},"print(repr(user_key))"," helps reveal hidden spaces like ",[18,1305,1306],{},"\"age \"",[329,1308,1309,1312],{},[18,1310,1311],{},"print(type(user_key))"," shows whether the key is a string, integer, or something else",[14,1314,1315],{},"Example:",[38,1317,1319],{"className":40,"code":1318,"language":42,"meta":43,"style":43},"data = {\"name\": \"Ana\", \"age \": 25}\nuser_key = \"age\"\n\nprint(data)\nprint(data.keys())\nprint(repr(user_key))\nprint(user_key in data)\n",[18,1320,1321,1358,1372,1376,1386,1400,1414],{"__ignoreMap":43},[47,1322,1323,1325,1327,1329,1331,1333,1335,1337,1339,1341,1343,1345,1347,1349,1351,1353,1356],{"class":49,"line":50},[47,1324,54],{"class":53},[47,1326,58],{"class":57},[47,1328,62],{"class":61},[47,1330,66],{"class":65},[47,1332,70],{"class":69},[47,1334,66],{"class":65},[47,1336,75],{"class":61},[47,1338,78],{"class":65},[47,1340,81],{"class":69},[47,1342,66],{"class":65},[47,1344,746],{"class":61},[47,1346,78],{"class":65},[47,1348,646],{"class":69},[47,1350,66],{"class":65},[47,1352,75],{"class":61},[47,1354,1355],{"class":749}," 25",[47,1357,86],{"class":61},[47,1359,1360,1363,1365,1367,1369],{"class":49,"line":89},[47,1361,1362],{"class":53},"user_key ",[47,1364,58],{"class":57},[47,1366,78],{"class":65},[47,1368,126],{"class":69},[47,1370,1371],{"class":65},"\"\n",[47,1373,1374],{"class":49,"line":96},[47,1375,93],{"emptyLinePlaceholder":92},[47,1377,1378,1380,1382,1384],{"class":49,"line":103},[47,1379,138],{"class":137},[47,1381,121],{"class":61},[47,1383,192],{"class":117},[47,1385,131],{"class":61},[47,1387,1388,1390,1392,1394,1396,1398],{"class":49,"line":134},[47,1389,138],{"class":137},[47,1391,121],{"class":61},[47,1393,192],{"class":117},[47,1395,114],{"class":61},[47,1397,1221],{"class":117},[47,1399,1224],{"class":61},[47,1401,1402,1404,1406,1408,1410,1412],{"class":49,"line":152},[47,1403,138],{"class":137},[47,1405,121],{"class":61},[47,1407,1251],{"class":137},[47,1409,121],{"class":61},[47,1411,1256],{"class":117},[47,1413,1259],{"class":61},[47,1415,1416,1418,1420,1422,1424,1426],{"class":49,"line":157},[47,1417,138],{"class":137},[47,1419,121],{"class":61},[47,1421,1362],{"class":117},[47,1423,238],{"class":166},[47,1425,111],{"class":117},[47,1427,131],{"class":61},[14,1429,549],{},[38,1431,1433],{"className":40,"code":1432,"language":42,"meta":43,"style":43},"{'name': 'Ana', 'age ': 25}\ndict_keys(['name', 'age '])\n'age'\nFalse\n",[18,1434,1435,1469,1493,1503],{"__ignoreMap":43},[47,1436,1437,1440,1443,1445,1447,1449,1451,1453,1455,1457,1459,1461,1463,1465,1467],{"class":49,"line":50},[47,1438,1439],{"class":61},"{",[47,1441,1442],{"class":65},"'",[47,1444,70],{"class":69},[47,1446,1442],{"class":65},[47,1448,75],{"class":61},[47,1450,316],{"class":65},[47,1452,81],{"class":69},[47,1454,1442],{"class":65},[47,1456,746],{"class":61},[47,1458,316],{"class":65},[47,1460,646],{"class":69},[47,1462,1442],{"class":65},[47,1464,75],{"class":61},[47,1466,1355],{"class":749},[47,1468,86],{"class":61},[47,1470,1471,1474,1477,1479,1481,1483,1485,1487,1489,1491],{"class":49,"line":89},[47,1472,1473],{"class":117},"dict_keys",[47,1475,1476],{"class":61},"([",[47,1478,1442],{"class":65},[47,1480,70],{"class":69},[47,1482,1442],{"class":65},[47,1484,746],{"class":61},[47,1486,316],{"class":65},[47,1488,646],{"class":69},[47,1490,1442],{"class":65},[47,1492,204],{"class":61},[47,1494,1495,1498,1501],{"class":49,"line":96},[47,1496,1442],{"class":1497},"s2W-s",[47,1499,126],{"class":1500},"sithA",[47,1502,321],{"class":1497},[47,1504,1505],{"class":49,"line":103},[47,1506,1507],{"class":687},"False\n",[14,1509,1510,1511,1513,1514,114],{},"Here the real key is ",[18,1512,1306],{}," with a trailing space, not ",[18,1515,340],{},[14,1517,1518,1519,114],{},"If your dictionary came from JSON, inspect the full data before assuming a field exists. See ",[348,1520,1522],{"href":1521},"\u002Fhow-to\u002Fhow-to-parse-json-in-python","how to parse JSON in Python",[33,1524,1526],{"id":1525},"common-real-examples","Common real examples",[14,1528,1529],{},"These are very common beginner cases:",[1531,1532,1534],"h3",{"id":1533},"reading-json-data-and-assuming-a-field-always-exists","Reading JSON data and assuming a field always exists",[38,1536,1538],{"className":40,"code":1537,"language":42,"meta":43,"style":43},"data = {\"name\": \"Ana\"}\n\nemail = data.get(\"email\")\nprint(email)\n",[18,1539,1540,1564,1568,1592],{"__ignoreMap":43},[47,1541,1542,1544,1546,1548,1550,1552,1554,1556,1558,1560,1562],{"class":49,"line":50},[47,1543,54],{"class":53},[47,1545,58],{"class":57},[47,1547,62],{"class":61},[47,1549,66],{"class":65},[47,1551,70],{"class":69},[47,1553,66],{"class":65},[47,1555,75],{"class":61},[47,1557,78],{"class":65},[47,1559,81],{"class":69},[47,1561,66],{"class":65},[47,1563,86],{"class":61},[47,1565,1566],{"class":49,"line":89},[47,1567,93],{"emptyLinePlaceholder":92},[47,1569,1570,1573,1575,1577,1579,1581,1583,1585,1588,1590],{"class":49,"line":96},[47,1571,1572],{"class":53},"email ",[47,1574,58],{"class":57},[47,1576,111],{"class":53},[47,1578,114],{"class":61},[47,1580,118],{"class":117},[47,1582,121],{"class":61},[47,1584,66],{"class":65},[47,1586,1587],{"class":69},"email",[47,1589,66],{"class":65},[47,1591,131],{"class":61},[47,1593,1594,1596,1598,1600],{"class":49,"line":103},[47,1595,138],{"class":137},[47,1597,121],{"class":61},[47,1599,1587],{"class":117},[47,1601,131],{"class":61},[14,1603,1604,1605,1608,1609,114],{},"If you used ",[18,1606,1607],{},"data[\"email\"]",", this would raise ",[18,1610,20],{},[1531,1612,1614],{"id":1613},"using-user-input-as-a-dictionary-key-without-checking-it","Using user input as a dictionary key without checking it",[38,1616,1618],{"className":40,"code":1617,"language":42,"meta":43,"style":43},"prices = {\"apple\": 2, \"banana\": 3}\n\nitem = \"orange\"\n\nif item in prices:\n    print(prices[item])\nelse:\n    print(\"Item not found\")\n",[18,1619,1620,1657,1661,1675,1679,1693,1709,1715],{"__ignoreMap":43},[47,1621,1622,1625,1627,1629,1631,1634,1636,1638,1641,1643,1645,1648,1650,1652,1655],{"class":49,"line":50},[47,1623,1624],{"class":53},"prices ",[47,1626,58],{"class":57},[47,1628,62],{"class":61},[47,1630,66],{"class":65},[47,1632,1633],{"class":69},"apple",[47,1635,66],{"class":65},[47,1637,75],{"class":61},[47,1639,1640],{"class":749}," 2",[47,1642,746],{"class":61},[47,1644,78],{"class":65},[47,1646,1647],{"class":69},"banana",[47,1649,66],{"class":65},[47,1651,75],{"class":61},[47,1653,1654],{"class":749}," 3",[47,1656,86],{"class":61},[47,1658,1659],{"class":49,"line":89},[47,1660,93],{"emptyLinePlaceholder":92},[47,1662,1663,1666,1668,1670,1673],{"class":49,"line":96},[47,1664,1665],{"class":53},"item ",[47,1667,58],{"class":57},[47,1669,78],{"class":65},[47,1671,1672],{"class":69},"orange",[47,1674,1371],{"class":65},[47,1676,1677],{"class":49,"line":103},[47,1678,93],{"emptyLinePlaceholder":92},[47,1680,1681,1683,1686,1688,1691],{"class":49,"line":134},[47,1682,167],{"class":166},[47,1684,1685],{"class":53}," item ",[47,1687,238],{"class":57},[47,1689,1690],{"class":53}," prices",[47,1692,181],{"class":61},[47,1694,1695,1697,1699,1702,1704,1707],{"class":49,"line":152},[47,1696,187],{"class":137},[47,1698,121],{"class":61},[47,1700,1701],{"class":117},"prices",[47,1703,195],{"class":61},[47,1705,1706],{"class":117},"item",[47,1708,204],{"class":61},[47,1710,1711,1713],{"class":49,"line":157},[47,1712,210],{"class":166},[47,1714,181],{"class":61},[47,1716,1717,1719,1721,1723,1726,1728],{"class":49,"line":163},[47,1718,187],{"class":137},[47,1720,121],{"class":61},[47,1722,66],{"class":65},[47,1724,1725],{"class":69},"Item not found",[47,1727,66],{"class":65},[47,1729,131],{"class":61},[1531,1731,1733],{"id":1732},"looking-up-a-key-after-removing-it","Looking up a key after removing it",[38,1735,1737],{"className":40,"code":1736,"language":42,"meta":43,"style":43},"data = {\"name\": \"Ana\", \"age\": 20}\n\ndel data[\"age\"]\nprint(data.get(\"age\"))\n",[18,1738,1739,1776,1780,1796],{"__ignoreMap":43},[47,1740,1741,1743,1745,1747,1749,1751,1753,1755,1757,1759,1761,1763,1765,1767,1769,1771,1774],{"class":49,"line":50},[47,1742,54],{"class":53},[47,1744,58],{"class":57},[47,1746,62],{"class":61},[47,1748,66],{"class":65},[47,1750,70],{"class":69},[47,1752,66],{"class":65},[47,1754,75],{"class":61},[47,1756,78],{"class":65},[47,1758,81],{"class":69},[47,1760,66],{"class":65},[47,1762,746],{"class":61},[47,1764,78],{"class":65},[47,1766,126],{"class":69},[47,1768,66],{"class":65},[47,1770,75],{"class":61},[47,1772,1773],{"class":749}," 20",[47,1775,86],{"class":61},[47,1777,1778],{"class":49,"line":89},[47,1779,93],{"emptyLinePlaceholder":92},[47,1781,1782,1784,1786,1788,1790,1792,1794],{"class":49,"line":96},[47,1783,379],{"class":166},[47,1785,111],{"class":53},[47,1787,195],{"class":61},[47,1789,66],{"class":65},[47,1791,126],{"class":69},[47,1793,66],{"class":65},[47,1795,1142],{"class":61},[47,1797,1798,1800,1802,1804,1806,1808,1810,1812,1814,1816],{"class":49,"line":103},[47,1799,138],{"class":137},[47,1801,121],{"class":61},[47,1803,192],{"class":117},[47,1805,114],{"class":61},[47,1807,118],{"class":117},[47,1809,121],{"class":61},[47,1811,66],{"class":65},[47,1813,126],{"class":69},[47,1815,66],{"class":65},[47,1817,1259],{"class":61},[1531,1819,1821],{"id":1820},"confusing-string-keys-and-integer-keys","Confusing string keys and integer keys",[38,1823,1825],{"className":40,"code":1824,"language":42,"meta":43,"style":43},"data = {\"1\": \"one\", 2: \"two\"}\n\nprint(data[\"1\"])  # works\nprint(data[2])    # works\n",[18,1826,1827,1866,1870,1891],{"__ignoreMap":43},[47,1828,1829,1831,1833,1835,1837,1840,1842,1844,1846,1849,1851,1853,1855,1857,1859,1862,1864],{"class":49,"line":50},[47,1830,54],{"class":53},[47,1832,58],{"class":57},[47,1834,62],{"class":61},[47,1836,66],{"class":65},[47,1838,1839],{"class":69},"1",[47,1841,66],{"class":65},[47,1843,75],{"class":61},[47,1845,78],{"class":65},[47,1847,1848],{"class":69},"one",[47,1850,66],{"class":65},[47,1852,746],{"class":61},[47,1854,1640],{"class":749},[47,1856,75],{"class":61},[47,1858,78],{"class":65},[47,1860,1861],{"class":69},"two",[47,1863,66],{"class":65},[47,1865,86],{"class":61},[47,1867,1868],{"class":49,"line":89},[47,1869,93],{"emptyLinePlaceholder":92},[47,1871,1872,1874,1876,1878,1880,1882,1884,1886,1888],{"class":49,"line":96},[47,1873,138],{"class":137},[47,1875,121],{"class":61},[47,1877,192],{"class":117},[47,1879,195],{"class":61},[47,1881,66],{"class":65},[47,1883,1839],{"class":69},[47,1885,66],{"class":65},[47,1887,435],{"class":61},[47,1889,1890],{"class":99},"  # works\n",[47,1892,1893,1895,1897,1899,1901,1904,1906],{"class":49,"line":103},[47,1894,138],{"class":137},[47,1896,121],{"class":61},[47,1898,192],{"class":117},[47,1900,195],{"class":61},[47,1902,1903],{"class":749},"2",[47,1905,435],{"class":61},[47,1907,1908],{"class":99},"    # works\n",[14,1910,1911],{},"But this would fail:",[38,1913,1915],{"className":40,"code":1914,"language":42,"meta":43,"style":43},"data = {\"1\": \"one\"}\n\nprint(data[1])\n",[18,1916,1917,1941,1945],{"__ignoreMap":43},[47,1918,1919,1921,1923,1925,1927,1929,1931,1933,1935,1937,1939],{"class":49,"line":50},[47,1920,54],{"class":53},[47,1922,58],{"class":57},[47,1924,62],{"class":61},[47,1926,66],{"class":65},[47,1928,1839],{"class":69},[47,1930,66],{"class":65},[47,1932,75],{"class":61},[47,1934,78],{"class":65},[47,1936,1848],{"class":69},[47,1938,66],{"class":65},[47,1940,86],{"class":61},[47,1942,1943],{"class":49,"line":89},[47,1944,93],{"emptyLinePlaceholder":92},[47,1946,1947,1949,1951,1953,1955,1957],{"class":49,"line":96},[47,1948,138],{"class":137},[47,1950,121],{"class":61},[47,1952,192],{"class":117},[47,1954,195],{"class":61},[47,1956,1839],{"class":749},[47,1958,204],{"class":61},[14,1960,549],{},[38,1962,1964],{"className":40,"code":1963,"language":42,"meta":43,"style":43},"KeyError: 1\n",[18,1965,1966],{"__ignoreMap":43},[47,1967,1968,1970,1972],{"class":49,"line":50},[47,1969,20],{"class":311},[47,1971,75],{"class":61},[47,1973,1974],{"class":749}," 1\n",[14,1976,1977,1978,1981,1982,1984],{},"The string ",[18,1979,1980],{},"\"1\""," and the integer ",[18,1983,1839],{}," are different keys.",[33,1986,1988],{"id":1987},"related-errors","Related errors",[14,1990,1991],{},"Some related pages may also help:",[326,1993,1994,2001,2008],{},[329,1995,1996,1997],{},"A broader explanation of this exception: ",[348,1998,2000],{"href":1999},"\u002Ferrors\u002Fkeyerror-in-python-causes-and-fixes","KeyError in Python: causes and fixes",[329,2002,2003,2004],{},"A closely related dictionary problem: ",[348,2005,2007],{"href":2006},"\u002Ferrors\u002Fkeyerror-key-not-found-in-dictionary-fix","KeyError: key not found in dictionary (fix)",[329,2009,2010,2011],{},"If you are working with dictionary methods, you may also need ",[348,2012,815],{"href":814},[33,2014,2016],{"id":2015},"common-mistakes","Common mistakes",[14,2018,2019,2020,75],{},"These are frequent causes of ",[18,2021,20],{},[326,2023,2024,2031,2040,2043,2046,2049],{},[329,2025,2026,2027,2030],{},"Using ",[18,2028,2029],{},"data[\"missing_key\"]"," when the key is not in the dictionary",[329,2032,2033,2034,2037,2038],{},"Wrong capitalization, such as ",[18,2035,2036],{},"\"Name\""," instead of ",[18,2039,334],{},[329,2041,2042],{},"Leading or trailing spaces in the key",[329,2044,2045],{},"Expecting a key from JSON or API data that is not always included",[329,2047,2048],{},"Mixing string keys and integer keys",[329,2050,2051],{},"Removing a key earlier in the program",[33,2053,2055],{"id":2054},"faq","FAQ",[1531,2057,2059,2060,2063,2064,2067],{"id":2058},"what-is-the-difference-between-datakey-and-datagetkey","What is the difference between ",[18,2061,2062],{},"data[\"key\"]"," and ",[18,2065,2066],{},"data.get(\"key\")","?",[14,2069,2070,2071,2073,2074,792,2077,2079],{},"Square brackets raise ",[18,2072,20],{}," if the key is missing. ",[18,2075,2076],{},"get()",[18,2078,795],{}," or a default value.",[1531,2081,2083,2084,2086],{"id":2082},"should-i-always-use-get-instead-of-square-brackets","Should I always use ",[18,2085,2076],{}," instead of square brackets?",[14,2088,2089,2090,2092],{},"No. Use square brackets when the key must exist. Use ",[18,2091,2076],{}," when the key might be missing.",[1531,2094,2096,2097,2099],{"id":2095},"can-keyerror-happen-with-json-data","Can ",[18,2098,20],{}," happen with JSON data?",[14,2101,2102,2103,114],{},"Yes. JSON objects become Python dictionaries, so missing fields can cause ",[18,2104,20],{},[1531,2106,2108],{"id":2107},"why-does-my-key-look-correct-but-still-fail","Why does my key look correct but still fail?",[14,2110,2111],{},"Check capitalization, spaces, and whether the key is a string or a number.",[33,2113,2115],{"id":2114},"see-also","See also",[326,2117,2118,2122,2126,2131,2136],{},[329,2119,2120],{},[348,2121,351],{"href":350},[329,2123,2124],{},[348,2125,815],{"href":814},[329,2127,2128],{},[348,2129,2130],{"href":597},"How to access values in a dictionary in Python",[329,2132,2133],{},[348,2134,2135],{"href":950},"How to check if a key exists in a dictionary in Python",[329,2137,2138],{},[348,2139,2140],{"href":1521},"How to parse JSON in Python",[14,2142,2143,2144,2146,2147,2149,2150,2152],{},"Once you fix this error, the next step is learning how to safely read dictionary data in everyday code. Using ",[18,2145,607],{},", checking with ",[18,2148,238],{},", and inspecting real input data will help you avoid ",[18,2151,20],{}," in the future.",[2154,2155,2156],"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 .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}html pre.shiki code .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .s2W-s, html code.shiki .s2W-s{--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#032F62;--shiki-default-font-style:inherit;--shiki-dark:#9ECBFF;--shiki-dark-font-style:inherit}html pre.shiki code .sithA, html code.shiki .sithA{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#032F62;--shiki-default-font-style:inherit;--shiki-dark:#9ECBFF;--shiki-dark-font-style:inherit}",{"title":43,"searchDepth":89,"depth":89,"links":2158},[2159,2160,2161,2162,2163,2164,2165,2166,2167,2173,2174,2175,2184],{"id":35,"depth":89,"text":36},{"id":242,"depth":89,"text":243},{"id":354,"depth":89,"text":355},{"id":489,"depth":89,"text":490},{"id":601,"depth":89,"text":602},{"id":818,"depth":89,"text":819},{"id":954,"depth":89,"text":955},{"id":1186,"depth":89,"text":1187},{"id":1525,"depth":89,"text":1526,"children":2168},[2169,2170,2171,2172],{"id":1533,"depth":96,"text":1534},{"id":1613,"depth":96,"text":1614},{"id":1732,"depth":96,"text":1733},{"id":1820,"depth":96,"text":1821},{"id":1987,"depth":89,"text":1988},{"id":2015,"depth":89,"text":2016},{"id":2054,"depth":89,"text":2055,"children":2176},[2177,2179,2181,2183],{"id":2058,"depth":96,"text":2178},"What is the difference between data[\"key\"] and data.get(\"key\")?",{"id":2082,"depth":96,"text":2180},"Should I always use get() instead of square brackets?",{"id":2095,"depth":96,"text":2182},"Can KeyError happen with JSON data?",{"id":2107,"depth":96,"text":2108},{"id":2114,"depth":89,"text":2115},"Master keyerror when accessing dictionary values fix in our comprehensive Python beginner guide.","md",{},"\u002Ferrors\u002Fkeyerror-when-accessing-dictionary-values-fix",{"title":5,"description":2185},"errors\u002Fkeyerror-when-accessing-dictionary-values-fix","kFzHN8ifAxDhsLspxm3F_8C06rqcc5ZDCBPWaz7bBj4",1777585504806]