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