[{"data":1,"prerenderedAt":1432},["ShallowReactive",2],{"doc-\u002Ferrors\u002Fkeyerror-popitem-dictionary-is-empty-fix":3},{"id":4,"title":5,"body":6,"description":1425,"extension":1426,"meta":1427,"navigation":71,"path":1428,"seo":1429,"stem":1430,"__hash__":1431},"content\u002Ferrors\u002Fkeyerror-popitem-dictionary-is-empty-fix.md","KeyError: popitem(): dictionary is empty (Fix)",{"type":7,"value":8,"toc":1401},"minimark",[9,13,26,29,34,40,143,154,158,163,166,188,196,200,206,230,233,298,301,324,334,338,341,358,361,419,422,426,429,434,437,528,530,556,561,574,578,584,657,663,667,670,752,758,762,768,771,836,845,852,856,859,874,877,976,978,987,990,994,999,1002,1064,1067,1096,1099,1126,1129,1212,1214,1256,1260,1263,1295,1302,1309,1313,1320,1326,1333,1339,1343,1351,1355,1361,1365,1391,1397],[10,11,5],"h1",{"id":12},"keyerror-popitem-dictionary-is-empty-fix",[14,15,16,17,21,22,25],"p",{},"Fix the Python error ",[18,19,20],"code",{},"\"KeyError: popitem(): dictionary is empty\"",". This error happens when you call ",[18,23,24],{},"popitem()"," on a dictionary that has no items left.",[14,27,28],{},"This page explains what the error means, shows a small example that causes it, and gives simple ways to avoid it.",[30,31,33],"h2",{"id":32},"quick-fix","Quick fix",[14,35,36,37,39],{},"Check that the dictionary is not empty before calling ",[18,38,24],{},":",[41,42,47],"pre",{"className":43,"code":44,"language":45,"meta":46,"style":46},"language-python shiki shiki-themes material-theme-lighter github-light github-dark","data = {}\n\nif data:\n    key, value = data.popitem()\nelse:\n    print(\"Dictionary is empty\")\n","python","",[18,48,49,66,73,86,112,120],{"__ignoreMap":46},[50,51,54,58,62],"span",{"class":52,"line":53},"line",1,[50,55,57],{"class":56},"su5hD","data ",[50,59,61],{"class":60},"smGrS","=",[50,63,65],{"class":64},"sP7_E"," {}\n",[50,67,69],{"class":52,"line":68},2,[50,70,72],{"emptyLinePlaceholder":71},true,"\n",[50,74,76,80,83],{"class":52,"line":75},3,[50,77,79],{"class":78},"sVHd0","if",[50,81,82],{"class":56}," data",[50,84,85],{"class":64},":\n",[50,87,89,92,95,98,100,102,105,109],{"class":52,"line":88},4,[50,90,91],{"class":56},"    key",[50,93,94],{"class":64},",",[50,96,97],{"class":56}," value ",[50,99,61],{"class":60},[50,101,82],{"class":56},[50,103,104],{"class":64},".",[50,106,108],{"class":107},"slqww","popitem",[50,110,111],{"class":64},"()\n",[50,113,115,118],{"class":52,"line":114},5,[50,116,117],{"class":78},"else",[50,119,85],{"class":64},[50,121,123,127,130,134,138,140],{"class":52,"line":122},6,[50,124,126],{"class":125},"sptTA","    print",[50,128,129],{"class":64},"(",[50,131,133],{"class":132},"sjJ54","\"",[50,135,137],{"class":136},"s_sjI","Dictionary is empty",[50,139,133],{"class":132},[50,141,142],{"class":64},")\n",[14,144,145,146,149,150,153],{},"An empty dictionary is ",[18,147,148],{},"False",", so ",[18,151,152],{},"if data:"," is a simple way to avoid this error.",[30,155,157],{"id":156},"what-this-error-means","What this error means",[14,159,160,162],{},[18,161,24],{}," is a dictionary method that removes and returns one key-value pair.",[14,164,165],{},"If the dictionary is empty, there is nothing to remove. Python raises:",[41,167,169],{"className":43,"code":168,"language":45,"meta":46,"style":46},"KeyError: 'popitem(): dictionary is empty'\n",[18,170,171],{"__ignoreMap":46},[50,172,173,177,179,182,185],{"class":52,"line":53},[50,174,176],{"class":175},"sZMiF","KeyError",[50,178,39],{"class":64},[50,180,181],{"class":132}," '",[50,183,184],{"class":136},"popitem(): dictionary is empty",[50,186,187],{"class":132},"'\n",[14,189,190,191,104],{},"If you are new to dictionaries, see ",[192,193,195],"a",{"href":194},"\u002Flearn\u002Fpython-dictionaries-explained","Python dictionaries explained",[30,197,199],{"id":198},"example-that-causes-the-error","Example that causes the error",[14,201,202,203,205],{},"This code creates an empty dictionary and then calls ",[18,204,24],{}," on it:",[41,207,209],{"className":43,"code":208,"language":45,"meta":46,"style":46},"data = {}\ndata.popitem()\n",[18,210,211,219],{"__ignoreMap":46},[50,212,213,215,217],{"class":52,"line":53},[50,214,57],{"class":56},[50,216,61],{"class":60},[50,218,65],{"class":64},[50,220,221,224,226,228],{"class":52,"line":68},[50,222,223],{"class":56},"data",[50,225,104],{"class":64},[50,227,108],{"class":107},[50,229,111],{"class":64},[14,231,232],{},"Output:",[41,234,236],{"className":43,"code":235,"language":45,"meta":46,"style":46},"Traceback (most recent call last):\n  File \"\u003Cstdin>\", line 2, in \u003Cmodule>\nKeyError: 'popitem(): dictionary is empty'\n",[18,237,238,251,286],{"__ignoreMap":46},[50,239,240,243,245,248],{"class":52,"line":53},[50,241,242],{"class":107},"Traceback ",[50,244,129],{"class":64},[50,246,247],{"class":107},"most recent call last",[50,249,250],{"class":64},"):\n",[50,252,253,256,258,261,263,265,268,272,274,277,280,283],{"class":52,"line":68},[50,254,255],{"class":56},"  File ",[50,257,133],{"class":132},[50,259,260],{"class":136},"\u003Cstdin>",[50,262,133],{"class":132},[50,264,94],{"class":64},[50,266,267],{"class":56}," line ",[50,269,271],{"class":270},"srdBf","2",[50,273,94],{"class":64},[50,275,276],{"class":60}," in",[50,278,279],{"class":60}," \u003C",[50,281,282],{"class":56},"module",[50,284,285],{"class":60},">\n",[50,287,288,290,292,294,296],{"class":52,"line":75},[50,289,176],{"class":175},[50,291,39],{"class":64},[50,293,181],{"class":132},[50,295,184],{"class":136},[50,297,187],{"class":132},[14,299,300],{},"Why this fails:",[302,303,304,314,319],"ul",{},[305,306,307,309,310,313],"li",{},[18,308,223],{}," is ",[18,311,312],{},"{}",", which means it has no key-value pairs",[305,315,316,318],{},[18,317,24],{}," tries to remove one pair",[305,320,321,322],{},"There is nothing to remove, so Python raises ",[18,323,176],{},[14,325,326,327,104],{},"If you want to learn more about how this method works, see the ",[192,328,330,331,333],{"href":329},"\u002Freference\u002Fpython-dictionary-popitem-method","Python dictionary ",[18,332,24],{}," method",[30,335,337],{"id":336},"why-it-happens","Why it happens",[14,339,340],{},"This error usually happens for one of these reasons:",[302,342,343,346,349,355],{},[305,344,345],{},"The dictionary started empty",[305,347,348],{},"Items were removed earlier in the program",[305,350,351,352,354],{},"A loop kept calling ",[18,353,24],{}," after the dictionary became empty",[305,356,357],{},"You expected data from another function, but it returned an empty dictionary",[14,359,360],{},"Here is a common loop mistake:",[41,362,364],{"className":43,"code":363,"language":45,"meta":46,"style":46},"data = {\"a\": 1}\n\nwhile True:\n    print(data.popitem())\n",[18,365,366,389,393,404],{"__ignoreMap":46},[50,367,368,370,372,375,377,379,381,383,386],{"class":52,"line":53},[50,369,57],{"class":56},[50,371,61],{"class":60},[50,373,374],{"class":64}," {",[50,376,133],{"class":132},[50,378,192],{"class":136},[50,380,133],{"class":132},[50,382,39],{"class":64},[50,384,385],{"class":270}," 1",[50,387,388],{"class":64},"}\n",[50,390,391],{"class":52,"line":68},[50,392,72],{"emptyLinePlaceholder":71},[50,394,395,398,402],{"class":52,"line":75},[50,396,397],{"class":78},"while",[50,399,401],{"class":400},"s39Yj"," True",[50,403,85],{"class":64},[50,405,406,408,410,412,414,416],{"class":52,"line":88},[50,407,126],{"class":125},[50,409,129],{"class":64},[50,411,223],{"class":107},[50,413,104],{"class":64},[50,415,108],{"class":107},[50,417,418],{"class":64},"())\n",[14,420,421],{},"This removes the only item first. On the next loop, the dictionary is empty, so the error happens.",[30,423,425],{"id":424},"how-to-fix-it","How to fix it",[14,427,428],{},"There are a few simple ways to fix this error.",[430,431,433],"h3",{"id":432},"check-if-the-dictionary-has-items","Check if the dictionary has items",[14,435,436],{},"This is the clearest fix for beginners:",[41,438,440],{"className":43,"code":439,"language":45,"meta":46,"style":46},"data = {\"name\": \"Alice\"}\n\nif data:\n    item = data.popitem()\n    print(item)\nelse:\n    print(\"Dictionary is empty\")\n",[18,441,442,469,473,481,496,507,513],{"__ignoreMap":46},[50,443,444,446,448,450,452,455,457,459,462,465,467],{"class":52,"line":53},[50,445,57],{"class":56},[50,447,61],{"class":60},[50,449,374],{"class":64},[50,451,133],{"class":132},[50,453,454],{"class":136},"name",[50,456,133],{"class":132},[50,458,39],{"class":64},[50,460,461],{"class":132}," \"",[50,463,464],{"class":136},"Alice",[50,466,133],{"class":132},[50,468,388],{"class":64},[50,470,471],{"class":52,"line":68},[50,472,72],{"emptyLinePlaceholder":71},[50,474,475,477,479],{"class":52,"line":75},[50,476,79],{"class":78},[50,478,82],{"class":56},[50,480,85],{"class":64},[50,482,483,486,488,490,492,494],{"class":52,"line":88},[50,484,485],{"class":56},"    item ",[50,487,61],{"class":60},[50,489,82],{"class":56},[50,491,104],{"class":64},[50,493,108],{"class":107},[50,495,111],{"class":64},[50,497,498,500,502,505],{"class":52,"line":114},[50,499,126],{"class":125},[50,501,129],{"class":64},[50,503,504],{"class":107},"item",[50,506,142],{"class":64},[50,508,509,511],{"class":52,"line":122},[50,510,117],{"class":78},[50,512,85],{"class":64},[50,514,516,518,520,522,524,526],{"class":52,"line":515},7,[50,517,126],{"class":125},[50,519,129],{"class":64},[50,521,133],{"class":132},[50,523,137],{"class":136},[50,525,133],{"class":132},[50,527,142],{"class":64},[14,529,232],{},[41,531,533],{"className":43,"code":532,"language":45,"meta":46,"style":46},"('name', 'Alice')\n",[18,534,535],{"__ignoreMap":46},[50,536,537,539,542,544,546,548,550,552,554],{"class":52,"line":53},[50,538,129],{"class":64},[50,540,541],{"class":132},"'",[50,543,454],{"class":136},[50,545,541],{"class":132},[50,547,94],{"class":64},[50,549,181],{"class":132},[50,551,464],{"class":136},[50,553,541],{"class":132},[50,555,142],{"class":64},[14,557,558,560],{},[18,559,152],{}," works because:",[302,562,563,569],{},[305,564,565,566],{},"non-empty dictionaries are ",[18,567,568],{},"True",[305,570,571,572],{},"empty dictionaries are ",[18,573,148],{},[430,575,577],{"id":576},"use-an-explicit-length-check","Use an explicit length check",[14,579,580,581,39],{},"If you want to be more explicit, use ",[18,582,583],{},"len()",[41,585,587],{"className":43,"code":586,"language":45,"meta":46,"style":46},"data = {}\n\nif len(data) > 0:\n    print(data.popitem())\nelse:\n    print(\"Dictionary is empty\")\n",[18,588,589,597,601,623,637,643],{"__ignoreMap":46},[50,590,591,593,595],{"class":52,"line":53},[50,592,57],{"class":56},[50,594,61],{"class":60},[50,596,65],{"class":64},[50,598,599],{"class":52,"line":68},[50,600,72],{"emptyLinePlaceholder":71},[50,602,603,605,608,610,612,615,618,621],{"class":52,"line":75},[50,604,79],{"class":78},[50,606,607],{"class":125}," len",[50,609,129],{"class":64},[50,611,223],{"class":107},[50,613,614],{"class":64},")",[50,616,617],{"class":60}," >",[50,619,620],{"class":270}," 0",[50,622,85],{"class":64},[50,624,625,627,629,631,633,635],{"class":52,"line":88},[50,626,126],{"class":125},[50,628,129],{"class":64},[50,630,223],{"class":107},[50,632,104],{"class":64},[50,634,108],{"class":107},[50,636,418],{"class":64},[50,638,639,641],{"class":52,"line":114},[50,640,117],{"class":78},[50,642,85],{"class":64},[50,644,645,647,649,651,653,655],{"class":52,"line":122},[50,646,126],{"class":125},[50,648,129],{"class":64},[50,650,133],{"class":132},[50,652,137],{"class":136},[50,654,133],{"class":132},[50,656,142],{"class":64},[14,658,659,660,662],{},"This does the same thing, but ",[18,661,152],{}," is shorter and more common.",[430,664,666],{"id":665},"use-try-except-when-empty-dictionaries-are-expected","Use try-except when empty dictionaries are expected",[14,668,669],{},"Sometimes an empty dictionary is normal in your program. In that case, you can catch the error:",[41,671,673],{"className":43,"code":672,"language":45,"meta":46,"style":46},"data = {}\n\ntry:\n    key, value = data.popitem()\n    print(key, value)\nexcept KeyError:\n    print(\"Dictionary is empty\")\n",[18,674,675,683,687,694,712,728,738],{"__ignoreMap":46},[50,676,677,679,681],{"class":52,"line":53},[50,678,57],{"class":56},[50,680,61],{"class":60},[50,682,65],{"class":64},[50,684,685],{"class":52,"line":68},[50,686,72],{"emptyLinePlaceholder":71},[50,688,689,692],{"class":52,"line":75},[50,690,691],{"class":78},"try",[50,693,85],{"class":64},[50,695,696,698,700,702,704,706,708,710],{"class":52,"line":88},[50,697,91],{"class":56},[50,699,94],{"class":64},[50,701,97],{"class":56},[50,703,61],{"class":60},[50,705,82],{"class":56},[50,707,104],{"class":64},[50,709,108],{"class":107},[50,711,111],{"class":64},[50,713,714,716,718,721,723,726],{"class":52,"line":114},[50,715,126],{"class":125},[50,717,129],{"class":64},[50,719,720],{"class":107},"key",[50,722,94],{"class":64},[50,724,725],{"class":107}," value",[50,727,142],{"class":64},[50,729,730,733,736],{"class":52,"line":122},[50,731,732],{"class":78},"except",[50,734,735],{"class":175}," KeyError",[50,737,85],{"class":64},[50,739,740,742,744,746,748,750],{"class":52,"line":515},[50,741,126],{"class":125},[50,743,129],{"class":64},[50,745,133],{"class":132},[50,747,137],{"class":136},[50,749,133],{"class":132},[50,751,142],{"class":64},[14,753,754,755,757],{},"This works, but for beginners, an ",[18,756,79],{}," check is usually easier to read.",[430,759,761],{"id":760},"review-earlier-code","Review earlier code",[14,763,764,765,767],{},"Sometimes ",[18,766,24],{}," is not the real problem. The real issue is that the dictionary became empty earlier.",[14,769,770],{},"For example:",[41,772,774],{"className":43,"code":773,"language":45,"meta":46,"style":46},"data = {\"x\": 1, \"y\": 2}\n\ndata.clear()\ndata.popitem()\n",[18,775,776,811,815,826],{"__ignoreMap":46},[50,777,778,780,782,784,786,789,791,793,795,797,799,802,804,806,809],{"class":52,"line":53},[50,779,57],{"class":56},[50,781,61],{"class":60},[50,783,374],{"class":64},[50,785,133],{"class":132},[50,787,788],{"class":136},"x",[50,790,133],{"class":132},[50,792,39],{"class":64},[50,794,385],{"class":270},[50,796,94],{"class":64},[50,798,461],{"class":132},[50,800,801],{"class":136},"y",[50,803,133],{"class":132},[50,805,39],{"class":64},[50,807,808],{"class":270}," 2",[50,810,388],{"class":64},[50,812,813],{"class":52,"line":68},[50,814,72],{"emptyLinePlaceholder":71},[50,816,817,819,821,824],{"class":52,"line":75},[50,818,223],{"class":56},[50,820,104],{"class":64},[50,822,823],{"class":107},"clear",[50,825,111],{"class":64},[50,827,828,830,832,834],{"class":52,"line":88},[50,829,223],{"class":56},[50,831,104],{"class":64},[50,833,108],{"class":107},[50,835,111],{"class":64},[14,837,838,841,842,844],{},[18,839,840],{},"data.clear()"," removes everything, so ",[18,843,24],{}," fails after that.",[14,846,847,848,104],{},"If you are removing a specific key instead of an arbitrary item, see ",[192,849,851],{"href":850},"\u002Fhow-to\u002Fhow-to-remove-a-key-from-a-dictionary-in-python","how to remove a key from a dictionary in Python",[30,853,855],{"id":854},"recommended-beginner-safe-pattern","Recommended beginner-safe pattern",[14,857,858],{},"The safest pattern is:",[860,861,862,865,871],"ol",{},[305,863,864],{},"Check whether the dictionary is empty",[305,866,867,868,870],{},"Use ",[18,869,24],{}," only when there is at least one item",[305,872,873],{},"Handle the empty case with a message, return value, or different logic",[14,875,876],{},"Example:",[41,878,880],{"className":43,"code":879,"language":45,"meta":46,"style":46},"def remove_one_item(data):\n    if data:\n        return data.popitem()\n    return None\n\ninfo = {}\nresult = remove_one_item(info)\n\nprint(result)\n",[18,881,882,899,908,921,929,933,942,958,963],{"__ignoreMap":46},[50,883,884,888,892,894,897],{"class":52,"line":53},[50,885,887],{"class":886},"sbsja","def",[50,889,891],{"class":890},"sGLFI"," remove_one_item",[50,893,129],{"class":64},[50,895,223],{"class":896},"sFwrP",[50,898,250],{"class":64},[50,900,901,904,906],{"class":52,"line":68},[50,902,903],{"class":78},"    if",[50,905,82],{"class":56},[50,907,85],{"class":64},[50,909,910,913,915,917,919],{"class":52,"line":75},[50,911,912],{"class":78},"        return",[50,914,82],{"class":56},[50,916,104],{"class":64},[50,918,108],{"class":107},[50,920,111],{"class":64},[50,922,923,926],{"class":52,"line":88},[50,924,925],{"class":78},"    return",[50,927,928],{"class":400}," None\n",[50,930,931],{"class":52,"line":114},[50,932,72],{"emptyLinePlaceholder":71},[50,934,935,938,940],{"class":52,"line":122},[50,936,937],{"class":56},"info ",[50,939,61],{"class":60},[50,941,65],{"class":64},[50,943,944,947,949,951,953,956],{"class":52,"line":515},[50,945,946],{"class":56},"result ",[50,948,61],{"class":60},[50,950,891],{"class":107},[50,952,129],{"class":64},[50,954,955],{"class":107},"info",[50,957,142],{"class":64},[50,959,961],{"class":52,"line":960},8,[50,962,72],{"emptyLinePlaceholder":71},[50,964,966,969,971,974],{"class":52,"line":965},9,[50,967,968],{"class":125},"print",[50,970,129],{"class":64},[50,972,973],{"class":107},"result",[50,975,142],{"class":64},[14,977,232],{},[41,979,981],{"className":43,"code":980,"language":45,"meta":46,"style":46},"None\n",[18,982,983],{"__ignoreMap":46},[50,984,985],{"class":52,"line":53},[50,986,980],{"class":400},[14,988,989],{},"This is usually clearer than relying on exceptions for normal program flow.",[30,991,993],{"id":992},"debugging-steps","Debugging steps",[14,995,996,997,104],{},"If you are not sure why the error is happening, inspect the dictionary right before ",[18,998,24],{},[14,1000,1001],{},"Useful checks:",[41,1003,1005],{"className":43,"code":1004,"language":45,"meta":46,"style":46},"print(my_dict)\nprint(len(my_dict))\nprint(bool(my_dict))\nprint(my_dict.keys())\n",[18,1006,1007,1018,1034,1049],{"__ignoreMap":46},[50,1008,1009,1011,1013,1016],{"class":52,"line":53},[50,1010,968],{"class":125},[50,1012,129],{"class":64},[50,1014,1015],{"class":107},"my_dict",[50,1017,142],{"class":64},[50,1019,1020,1022,1024,1027,1029,1031],{"class":52,"line":68},[50,1021,968],{"class":125},[50,1023,129],{"class":64},[50,1025,1026],{"class":125},"len",[50,1028,129],{"class":64},[50,1030,1015],{"class":107},[50,1032,1033],{"class":64},"))\n",[50,1035,1036,1038,1040,1043,1045,1047],{"class":52,"line":75},[50,1037,968],{"class":125},[50,1039,129],{"class":64},[50,1041,1042],{"class":175},"bool",[50,1044,129],{"class":64},[50,1046,1015],{"class":107},[50,1048,1033],{"class":64},[50,1050,1051,1053,1055,1057,1059,1062],{"class":52,"line":88},[50,1052,968],{"class":125},[50,1054,129],{"class":64},[50,1056,1015],{"class":107},[50,1058,104],{"class":64},[50,1060,1061],{"class":107},"keys",[50,1063,418],{"class":64},[14,1065,1066],{},"What these show:",[302,1068,1069,1075,1081,1090],{},[305,1070,1071,1074],{},[18,1072,1073],{},"print(my_dict)"," shows the full dictionary",[305,1076,1077,1080],{},[18,1078,1079],{},"print(len(my_dict))"," shows how many items it has",[305,1082,1083,1086,1087,1089],{},[18,1084,1085],{},"print(bool(my_dict))"," shows ",[18,1088,148],{}," when it is empty",[305,1091,1092,1095],{},[18,1093,1094],{},"print(my_dict.keys())"," shows the current keys",[14,1097,1098],{},"You can also:",[302,1100,1101,1106,1109,1123],{},[305,1102,1103,1104],{},"Print the dictionary right before ",[18,1105,24],{},[305,1107,1108],{},"Trace where items are removed earlier",[305,1110,1111,1112,1115,1116,1119,1120,1122],{},"Check whether ",[18,1113,1114],{},"clear()",", ",[18,1117,1118],{},"pop()",", or another ",[18,1121,24],{}," emptied it",[305,1124,1125],{},"If using a loop, make sure the loop stops when the dictionary becomes empty",[14,1127,1128],{},"A safe loop looks like this:",[41,1130,1132],{"className":43,"code":1131,"language":45,"meta":46,"style":46},"data = {\"a\": 1, \"b\": 2}\n\nwhile data:\n    print(data.popitem())\n\nprint(\"Done\")\n",[18,1133,1134,1167,1171,1179,1193,1197],{"__ignoreMap":46},[50,1135,1136,1138,1140,1142,1144,1146,1148,1150,1152,1154,1156,1159,1161,1163,1165],{"class":52,"line":53},[50,1137,57],{"class":56},[50,1139,61],{"class":60},[50,1141,374],{"class":64},[50,1143,133],{"class":132},[50,1145,192],{"class":136},[50,1147,133],{"class":132},[50,1149,39],{"class":64},[50,1151,385],{"class":270},[50,1153,94],{"class":64},[50,1155,461],{"class":132},[50,1157,1158],{"class":136},"b",[50,1160,133],{"class":132},[50,1162,39],{"class":64},[50,1164,808],{"class":270},[50,1166,388],{"class":64},[50,1168,1169],{"class":52,"line":68},[50,1170,72],{"emptyLinePlaceholder":71},[50,1172,1173,1175,1177],{"class":52,"line":75},[50,1174,397],{"class":78},[50,1176,82],{"class":56},[50,1178,85],{"class":64},[50,1180,1181,1183,1185,1187,1189,1191],{"class":52,"line":88},[50,1182,126],{"class":125},[50,1184,129],{"class":64},[50,1186,223],{"class":107},[50,1188,104],{"class":64},[50,1190,108],{"class":107},[50,1192,418],{"class":64},[50,1194,1195],{"class":52,"line":114},[50,1196,72],{"emptyLinePlaceholder":71},[50,1198,1199,1201,1203,1205,1208,1210],{"class":52,"line":122},[50,1200,968],{"class":125},[50,1202,129],{"class":64},[50,1204,133],{"class":132},[50,1206,1207],{"class":136},"Done",[50,1209,133],{"class":132},[50,1211,142],{"class":64},[14,1213,232],{},[41,1215,1217],{"className":43,"code":1216,"language":45,"meta":46,"style":46},"('b', 2)\n('a', 1)\nDone\n",[18,1218,1219,1235,1251],{"__ignoreMap":46},[50,1220,1221,1223,1225,1227,1229,1231,1233],{"class":52,"line":53},[50,1222,129],{"class":64},[50,1224,541],{"class":132},[50,1226,1158],{"class":136},[50,1228,541],{"class":132},[50,1230,94],{"class":64},[50,1232,808],{"class":270},[50,1234,142],{"class":64},[50,1236,1237,1239,1241,1243,1245,1247,1249],{"class":52,"line":68},[50,1238,129],{"class":64},[50,1240,541],{"class":132},[50,1242,192],{"class":136},[50,1244,541],{"class":132},[50,1246,94],{"class":64},[50,1248,385],{"class":270},[50,1250,142],{"class":64},[50,1252,1253],{"class":52,"line":75},[50,1254,1255],{"class":56},"Done\n",[30,1257,1259],{"id":1258},"common-mistakes","Common mistakes",[14,1261,1262],{},"These are common causes of this error:",[302,1264,1265,1273,1282,1292],{},[305,1266,1267,1268,1270,1271],{},"Calling ",[18,1269,24],{}," on ",[18,1272,312],{},[305,1274,1275,1276,1278,1279,1281],{},"Using ",[18,1277,24],{}," inside a ",[18,1280,397],{}," loop without checking if items remain",[305,1283,1284,1285,1115,1287,1289,1290],{},"Removing all dictionary items earlier with ",[18,1286,1114],{},[18,1288,1118],{},", or ",[18,1291,24],{},[305,1293,1294],{},"Assuming loaded or returned data is non-empty when it is actually empty",[14,1296,1297,1298,104],{},"If you are dealing with a broader dictionary key problem, see ",[192,1299,1301],{"href":1300},"\u002Ferrors\u002Fkeyerror-in-python-causes-and-fixes","KeyError in Python: causes and fixes",[14,1303,1304,1305,104],{},"If your code depends on checking for a key before removing or reading it, see ",[192,1306,1308],{"href":1307},"\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",[30,1310,1312],{"id":1311},"faq","FAQ",[430,1314,1316,1317,1319],{"id":1315},"what-does-popitem-return","What does ",[18,1318,24],{}," return?",[14,1321,1322,1323,104],{},"It returns one key-value pair as a tuple, like ",[18,1324,1325],{},"('name', 'Alice')",[430,1327,1329,1330,1332],{"id":1328},"why-is-this-a-keyerror","Why is this a ",[18,1331,176],{},"?",[14,1334,1335,1336,1338],{},"Because ",[18,1337,24],{}," tries to remove a dictionary key, but there is no key to remove.",[430,1340,1342],{"id":1341},"how-do-i-check-if-a-dictionary-is-empty","How do I check if a dictionary is empty?",[14,1344,867,1345,1348,1349,104],{},[18,1346,1347],{},"if my_dict:",". An empty dictionary is ",[18,1350,148],{},[430,1352,1354],{"id":1353},"should-i-use-try-except-or-an-if-check","Should I use try-except or an if check?",[14,1356,1357,1358,1360],{},"For beginners, an ",[18,1359,79],{}," check is usually simpler and easier to read.",[30,1362,1364],{"id":1363},"see-also","See also",[302,1366,1367,1371,1377,1382,1387],{},[305,1368,1369],{},[192,1370,195],{"href":194},[305,1372,1373],{},[192,1374,330,1375,333],{"href":329},[18,1376,24],{},[305,1378,1379],{},[192,1380,1381],{"href":850},"How to remove a key from a dictionary in Python",[305,1383,1384],{},[192,1385,1386],{"href":1307},"How to check if a key exists in a dictionary in Python",[305,1388,1389],{},[192,1390,1301],{"href":1300},[14,1392,1393,1394,1396],{},"To prevent this error next time, learn how dictionaries work and use ",[18,1395,24],{}," only when you know the dictionary still has items.",[1398,1399,1400],"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 .sVHd0, html code.shiki .sVHd0{--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#D73A49;--shiki-default-font-style:inherit;--shiki-dark:#F97583;--shiki-dark-font-style:inherit}html pre.shiki code .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 .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 .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 .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--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 .sbsja, html code.shiki .sbsja{--shiki-light:#9C3EDA;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sGLFI, html code.shiki .sGLFI{--shiki-light:#6182B8;--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .sFwrP, html code.shiki .sFwrP{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#24292E;--shiki-default-font-style:inherit;--shiki-dark:#E1E4E8;--shiki-dark-font-style:inherit}",{"title":46,"searchDepth":68,"depth":68,"links":1402},[1403,1404,1405,1406,1407,1413,1414,1415,1416,1424],{"id":32,"depth":68,"text":33},{"id":156,"depth":68,"text":157},{"id":198,"depth":68,"text":199},{"id":336,"depth":68,"text":337},{"id":424,"depth":68,"text":425,"children":1408},[1409,1410,1411,1412],{"id":432,"depth":75,"text":433},{"id":576,"depth":75,"text":577},{"id":665,"depth":75,"text":666},{"id":760,"depth":75,"text":761},{"id":854,"depth":68,"text":855},{"id":992,"depth":68,"text":993},{"id":1258,"depth":68,"text":1259},{"id":1311,"depth":68,"text":1312,"children":1417},[1418,1420,1422,1423],{"id":1315,"depth":75,"text":1419},"What does popitem() return?",{"id":1328,"depth":75,"text":1421},"Why is this a KeyError?",{"id":1341,"depth":75,"text":1342},{"id":1353,"depth":75,"text":1354},{"id":1363,"depth":68,"text":1364},"Master keyerror popitem dictionary is empty fix in our comprehensive Python beginner guide.","md",{},"\u002Ferrors\u002Fkeyerror-popitem-dictionary-is-empty-fix",{"title":5,"description":1425},"errors\u002Fkeyerror-popitem-dictionary-is-empty-fix","KuoZaFk4N1fx9ZUp2mVnm7POobapu3tBtzYJa-UU9fw",1777585480246]