[{"data":1,"prerenderedAt":1638},["ShallowReactive",2],{"doc-\u002Freference\u002Fpython-dictionary-clear-method":3},{"id":4,"title":5,"body":6,"description":1631,"extension":1632,"meta":1633,"navigation":312,"path":1634,"seo":1635,"stem":1636,"__hash__":1637},"content\u002Freference\u002Fpython-dictionary-clear-method.md","Python Dictionary clear() Method",{"type":7,"value":8,"toc":1600},"minimark",[9,19,26,34,39,136,141,148,154,176,179,186,190,206,209,222,226,229,378,381,451,454,470,476,483,486,571,574,590,595,602,667,669,677,680,703,709,772,778,781,795,808,812,823,828,839,846,850,856,979,981,1008,1011,1029,1032,1143,1145,1195,1198,1211,1220,1226,1231,1242,1245,1349,1373,1377,1382,1402,1405,1466,1469,1495,1499,1506,1509,1516,1521,1530,1538,1544,1547,1551,1596],[10,11,13,14,18],"h1",{"id":12},"python-dictionary-clear-method","Python Dictionary ",[15,16,17],"code",{},"clear()"," Method",[20,21,22,25],"p",{},[15,23,24],{},"dict.clear()"," removes all items from a dictionary.",[20,27,28,29,33],{},"Use it when you want to empty a dictionary without replacing the variable with a new dictionary. This method changes the original dictionary ",[30,31,32],"strong",{},"in place",", which means the same dictionary object is kept, but all key-value pairs are removed.",[35,36,38],"h2",{"id":37},"quick-example","Quick example",[40,41,46],"pre",{"className":42,"code":43,"language":44,"meta":45,"style":45},"language-python shiki shiki-themes material-theme-lighter github-light github-dark","data = {\"a\": 1, \"b\": 2}\ndata.clear()\nprint(data)  # {}\n","python","",[15,47,48,101,117],{"__ignoreMap":45},[49,50,53,57,61,65,69,73,75,78,82,85,88,91,93,95,98],"span",{"class":51,"line":52},"line",1,[49,54,56],{"class":55},"su5hD","data ",[49,58,60],{"class":59},"smGrS","=",[49,62,64],{"class":63},"sP7_E"," {",[49,66,68],{"class":67},"sjJ54","\"",[49,70,72],{"class":71},"s_sjI","a",[49,74,68],{"class":67},[49,76,77],{"class":63},":",[49,79,81],{"class":80},"srdBf"," 1",[49,83,84],{"class":63},",",[49,86,87],{"class":67}," \"",[49,89,90],{"class":71},"b",[49,92,68],{"class":67},[49,94,77],{"class":63},[49,96,97],{"class":80}," 2",[49,99,100],{"class":63},"}\n",[49,102,104,107,110,114],{"class":51,"line":103},2,[49,105,106],{"class":55},"data",[49,108,109],{"class":63},".",[49,111,113],{"class":112},"slqww","clear",[49,115,116],{"class":63},"()\n",[49,118,120,124,127,129,132],{"class":51,"line":119},3,[49,121,123],{"class":122},"sptTA","print",[49,125,126],{"class":63},"(",[49,128,106],{"class":112},[49,130,131],{"class":63},")",[49,133,135],{"class":134},"sutJx","  # {}\n",[20,137,138,140],{},[15,139,17],{}," removes all items from the same dictionary object.",[35,142,144,145,147],{"id":143},"what-clear-does","What ",[15,146,17],{}," does",[20,149,150,151,153],{},"The ",[15,152,17],{}," method:",[155,156,157,161,164,170],"ul",{},[158,159,160],"li",{},"Removes all key-value pairs from a dictionary",[158,162,163],{},"Changes the original dictionary in place",[158,165,166,167],{},"Returns ",[15,168,169],{},"None",[158,171,172,173],{},"Leaves you with an empty dictionary: ",[15,174,175],{},"{}",[20,177,178],{},"This is one of the standard ways to remove dictionary contents without deleting the variable itself.",[20,180,181,182,109],{},"If you are new to dictionaries, see ",[72,183,185],{"href":184},"\u002Flearn\u002Fpython-dictionaries-explained\u002F","Python dictionaries explained",[35,187,189],{"id":188},"basic-syntax","Basic syntax",[40,191,193],{"className":42,"code":192,"language":44,"meta":45,"style":45},"my_dict.clear()\n",[15,194,195],{"__ignoreMap":45},[49,196,197,200,202,204],{"class":51,"line":52},[49,198,199],{"class":55},"my_dict",[49,201,109],{"class":63},[49,203,113],{"class":112},[49,205,116],{"class":63},[20,207,208],{},"Important points:",[155,210,211,216,219],{},[158,212,213,215],{},[15,214,17],{}," takes no arguments",[158,217,218],{},"You call it on a dictionary object",[158,220,221],{},"It empties that dictionary",[35,223,225],{"id":224},"simple-example","Simple example",[20,227,228],{},"Here is a basic example:",[40,230,232],{"className":42,"code":231,"language":44,"meta":45,"style":45},"user = {\n    \"name\": \"Maya\",\n    \"age\": 25,\n    \"city\": \"Berlin\"\n}\n\nprint(\"Before:\", user)\n\nuser.clear()\n\nprint(\"After:\", user)\n",[15,233,234,244,266,282,302,307,314,336,341,353,358],{"__ignoreMap":45},[49,235,236,239,241],{"class":51,"line":52},[49,237,238],{"class":55},"user ",[49,240,60],{"class":59},[49,242,243],{"class":63}," {\n",[49,245,246,249,252,254,256,258,261,263],{"class":51,"line":103},[49,247,248],{"class":67},"    \"",[49,250,251],{"class":71},"name",[49,253,68],{"class":67},[49,255,77],{"class":63},[49,257,87],{"class":67},[49,259,260],{"class":71},"Maya",[49,262,68],{"class":67},[49,264,265],{"class":63},",\n",[49,267,268,270,273,275,277,280],{"class":51,"line":119},[49,269,248],{"class":67},[49,271,272],{"class":71},"age",[49,274,68],{"class":67},[49,276,77],{"class":63},[49,278,279],{"class":80}," 25",[49,281,265],{"class":63},[49,283,285,287,290,292,294,296,299],{"class":51,"line":284},4,[49,286,248],{"class":67},[49,288,289],{"class":71},"city",[49,291,68],{"class":67},[49,293,77],{"class":63},[49,295,87],{"class":67},[49,297,298],{"class":71},"Berlin",[49,300,301],{"class":67},"\"\n",[49,303,305],{"class":51,"line":304},5,[49,306,100],{"class":63},[49,308,310],{"class":51,"line":309},6,[49,311,313],{"emptyLinePlaceholder":312},true,"\n",[49,315,317,319,321,323,326,328,330,333],{"class":51,"line":316},7,[49,318,123],{"class":122},[49,320,126],{"class":63},[49,322,68],{"class":67},[49,324,325],{"class":71},"Before:",[49,327,68],{"class":67},[49,329,84],{"class":63},[49,331,332],{"class":112}," user",[49,334,335],{"class":63},")\n",[49,337,339],{"class":51,"line":338},8,[49,340,313],{"emptyLinePlaceholder":312},[49,342,344,347,349,351],{"class":51,"line":343},9,[49,345,346],{"class":55},"user",[49,348,109],{"class":63},[49,350,113],{"class":112},[49,352,116],{"class":63},[49,354,356],{"class":51,"line":355},10,[49,357,313],{"emptyLinePlaceholder":312},[49,359,361,363,365,367,370,372,374,376],{"class":51,"line":360},11,[49,362,123],{"class":122},[49,364,126],{"class":63},[49,366,68],{"class":67},[49,368,369],{"class":71},"After:",[49,371,68],{"class":67},[49,373,84],{"class":63},[49,375,332],{"class":112},[49,377,335],{"class":63},[20,379,380],{},"Expected output:",[40,382,384],{"className":42,"code":383,"language":44,"meta":45,"style":45},"Before: {'name': 'Maya', 'age': 25, 'city': 'Berlin'}\nAfter: {}\n",[15,385,386,441],{"__ignoreMap":45},[49,387,388,391,393,395,398,400,402,404,407,409,411,413,415,417,419,421,423,425,427,429,431,433,435,437,439],{"class":51,"line":52},[49,389,390],{"class":55},"Before",[49,392,77],{"class":63},[49,394,64],{"class":63},[49,396,397],{"class":67},"'",[49,399,251],{"class":71},[49,401,397],{"class":67},[49,403,77],{"class":63},[49,405,406],{"class":67}," '",[49,408,260],{"class":71},[49,410,397],{"class":67},[49,412,84],{"class":63},[49,414,406],{"class":67},[49,416,272],{"class":71},[49,418,397],{"class":67},[49,420,77],{"class":63},[49,422,279],{"class":80},[49,424,84],{"class":63},[49,426,406],{"class":67},[49,428,289],{"class":71},[49,430,397],{"class":67},[49,432,77],{"class":63},[49,434,406],{"class":67},[49,436,298],{"class":71},[49,438,397],{"class":67},[49,440,100],{"class":63},[49,442,443,446,448],{"class":51,"line":103},[49,444,445],{"class":55},"After",[49,447,77],{"class":63},[49,449,450],{"class":63}," {}\n",[20,452,453],{},"What happens here:",[155,455,456,461,467],{},[158,457,458,460],{},[15,459,346],{}," starts with three items",[158,462,463,466],{},[15,464,465],{},"user.clear()"," removes them all",[158,468,469],{},"The dictionary still exists, but it is now empty",[35,471,144,473,475],{"id":472},"what-clear-returns",[15,474,17],{}," returns",[20,477,478,480,481,109],{},[15,479,17],{}," returns ",[15,482,169],{},[20,484,485],{},"This is a very common beginner mistake:",[40,487,489],{"className":42,"code":488,"language":44,"meta":45,"style":45},"my_dict = {\"x\": 1, \"y\": 2}\n\nresult = my_dict.clear()\n\nprint(result)\nprint(my_dict)\n",[15,490,491,526,530,546,550,561],{"__ignoreMap":45},[49,492,493,496,498,500,502,505,507,509,511,513,515,518,520,522,524],{"class":51,"line":52},[49,494,495],{"class":55},"my_dict ",[49,497,60],{"class":59},[49,499,64],{"class":63},[49,501,68],{"class":67},[49,503,504],{"class":71},"x",[49,506,68],{"class":67},[49,508,77],{"class":63},[49,510,81],{"class":80},[49,512,84],{"class":63},[49,514,87],{"class":67},[49,516,517],{"class":71},"y",[49,519,68],{"class":67},[49,521,77],{"class":63},[49,523,97],{"class":80},[49,525,100],{"class":63},[49,527,528],{"class":51,"line":103},[49,529,313],{"emptyLinePlaceholder":312},[49,531,532,535,537,540,542,544],{"class":51,"line":119},[49,533,534],{"class":55},"result ",[49,536,60],{"class":59},[49,538,539],{"class":55}," my_dict",[49,541,109],{"class":63},[49,543,113],{"class":112},[49,545,116],{"class":63},[49,547,548],{"class":51,"line":284},[49,549,313],{"emptyLinePlaceholder":312},[49,551,552,554,556,559],{"class":51,"line":304},[49,553,123],{"class":122},[49,555,126],{"class":63},[49,557,558],{"class":112},"result",[49,560,335],{"class":63},[49,562,563,565,567,569],{"class":51,"line":309},[49,564,123],{"class":122},[49,566,126],{"class":63},[49,568,199],{"class":112},[49,570,335],{"class":63},[20,572,573],{},"Output:",[40,575,577],{"className":42,"code":576,"language":44,"meta":45,"style":45},"None\n{}\n",[15,578,579,585],{"__ignoreMap":45},[49,580,581],{"class":51,"line":52},[49,582,584],{"class":583},"s39Yj","None\n",[49,586,587],{"class":51,"line":103},[49,588,589],{"class":63},"{}\n",[591,592,594],"h3",{"id":593},"common-mistake","Common mistake",[20,596,597,598,601],{},"Do ",[30,599,600],{},"not"," do this:",[40,603,605],{"className":42,"code":604,"language":44,"meta":45,"style":45},"my_dict = {\"x\": 1, \"y\": 2}\nmy_dict = my_dict.clear()\n\nprint(my_dict)\n",[15,606,607,639,653,657],{"__ignoreMap":45},[49,608,609,611,613,615,617,619,621,623,625,627,629,631,633,635,637],{"class":51,"line":52},[49,610,495],{"class":55},[49,612,60],{"class":59},[49,614,64],{"class":63},[49,616,68],{"class":67},[49,618,504],{"class":71},[49,620,68],{"class":67},[49,622,77],{"class":63},[49,624,81],{"class":80},[49,626,84],{"class":63},[49,628,87],{"class":67},[49,630,517],{"class":71},[49,632,68],{"class":67},[49,634,77],{"class":63},[49,636,97],{"class":80},[49,638,100],{"class":63},[49,640,641,643,645,647,649,651],{"class":51,"line":103},[49,642,495],{"class":55},[49,644,60],{"class":59},[49,646,539],{"class":55},[49,648,109],{"class":63},[49,650,113],{"class":112},[49,652,116],{"class":63},[49,654,655],{"class":51,"line":119},[49,656,313],{"emptyLinePlaceholder":312},[49,658,659,661,663,665],{"class":51,"line":284},[49,660,123],{"class":122},[49,662,126],{"class":63},[49,664,199],{"class":112},[49,666,335],{"class":63},[20,668,573],{},[40,670,671],{"className":42,"code":584,"language":44,"meta":45,"style":45},[15,672,673],{"__ignoreMap":45},[49,674,675],{"class":51,"line":52},[49,676,584],{"class":583},[20,678,679],{},"Why this happens:",[155,681,682,688,693],{},[158,683,684,687],{},[15,685,686],{},"my_dict.clear()"," empties the dictionary",[158,689,690,691],{},"The method returns ",[15,692,169],{},[158,694,695,696,699,700,702],{},"Then ",[15,697,698],{},"my_dict = ..."," stores ",[15,701,169],{}," in the variable",[20,704,705,706,708],{},"If you want to keep using the dictionary, just call ",[15,707,17],{}," by itself:",[40,710,712],{"className":42,"code":711,"language":44,"meta":45,"style":45},"my_dict = {\"x\": 1, \"y\": 2}\nmy_dict.clear()\n\nprint(my_dict)  # {}\n",[15,713,714,746,756,760],{"__ignoreMap":45},[49,715,716,718,720,722,724,726,728,730,732,734,736,738,740,742,744],{"class":51,"line":52},[49,717,495],{"class":55},[49,719,60],{"class":59},[49,721,64],{"class":63},[49,723,68],{"class":67},[49,725,504],{"class":71},[49,727,68],{"class":67},[49,729,77],{"class":63},[49,731,81],{"class":80},[49,733,84],{"class":63},[49,735,87],{"class":67},[49,737,517],{"class":71},[49,739,68],{"class":67},[49,741,77],{"class":63},[49,743,97],{"class":80},[49,745,100],{"class":63},[49,747,748,750,752,754],{"class":51,"line":103},[49,749,199],{"class":55},[49,751,109],{"class":63},[49,753,113],{"class":112},[49,755,116],{"class":63},[49,757,758],{"class":51,"line":119},[49,759,313],{"emptyLinePlaceholder":312},[49,761,762,764,766,768,770],{"class":51,"line":284},[49,763,123],{"class":122},[49,765,126],{"class":63},[49,767,199],{"class":112},[49,769,131],{"class":63},[49,771,135],{"class":134},[35,773,775,777],{"id":774},"clear-vs-creating-a-new-empty-dictionary",[15,776,17],{}," vs creating a new empty dictionary",[20,779,780],{},"These two lines may look similar, but they do different things:",[40,782,783],{"className":42,"code":192,"language":44,"meta":45,"style":45},[15,784,785],{"__ignoreMap":45},[49,786,787,789,791,793],{"class":51,"line":52},[49,788,199],{"class":55},[49,790,109],{"class":63},[49,792,113],{"class":112},[49,794,116],{"class":63},[40,796,798],{"className":42,"code":797,"language":44,"meta":45,"style":45},"my_dict = {}\n",[15,799,800],{"__ignoreMap":45},[49,801,802,804,806],{"class":51,"line":52},[49,803,495],{"class":55},[49,805,60],{"class":59},[49,807,450],{"class":63},[591,809,810],{"id":113},[15,811,17],{},[155,813,814,817,820],{},[158,815,816],{},"Empties the existing dictionary object",[158,818,819],{},"Keeps the same object in memory",[158,821,822],{},"Affects all variables that refer to that same dictionary",[591,824,825],{"id":199},[15,826,827],{},"my_dict = {}",[155,829,830,833,836],{},[158,831,832],{},"Creates a new empty dictionary object",[158,834,835],{},"Reassigns the variable to that new object",[158,837,838],{},"Other variables pointing to the old dictionary are unchanged",[20,840,841,842,109],{},"This difference matters when you are working with shared references. It is also related to ",[72,843,845],{"href":844},"\u002Flearn\u002Fmutability-in-python-explained-mutable-vs-immutable-types\u002F","mutable vs immutable types in Python",[35,847,849],{"id":848},"shared-reference-example","Shared reference example",[20,851,852,853,855],{},"If two variables point to the same dictionary, ",[15,854,17],{}," affects both because both names refer to the same object.",[40,857,859],{"className":42,"code":858,"language":44,"meta":45,"style":45},"original = {\"a\": 1, \"b\": 2}\nother_ref = original\n\noriginal.clear()\n\nprint(\"original:\", original)\nprint(\"other_ref:\", other_ref)\nprint(original is other_ref)\n",[15,860,861,894,904,908,919,923,943,963],{"__ignoreMap":45},[49,862,863,866,868,870,872,874,876,878,880,882,884,886,888,890,892],{"class":51,"line":52},[49,864,865],{"class":55},"original ",[49,867,60],{"class":59},[49,869,64],{"class":63},[49,871,68],{"class":67},[49,873,72],{"class":71},[49,875,68],{"class":67},[49,877,77],{"class":63},[49,879,81],{"class":80},[49,881,84],{"class":63},[49,883,87],{"class":67},[49,885,90],{"class":71},[49,887,68],{"class":67},[49,889,77],{"class":63},[49,891,97],{"class":80},[49,893,100],{"class":63},[49,895,896,899,901],{"class":51,"line":103},[49,897,898],{"class":55},"other_ref ",[49,900,60],{"class":59},[49,902,903],{"class":55}," original\n",[49,905,906],{"class":51,"line":119},[49,907,313],{"emptyLinePlaceholder":312},[49,909,910,913,915,917],{"class":51,"line":284},[49,911,912],{"class":55},"original",[49,914,109],{"class":63},[49,916,113],{"class":112},[49,918,116],{"class":63},[49,920,921],{"class":51,"line":304},[49,922,313],{"emptyLinePlaceholder":312},[49,924,925,927,929,931,934,936,938,941],{"class":51,"line":309},[49,926,123],{"class":122},[49,928,126],{"class":63},[49,930,68],{"class":67},[49,932,933],{"class":71},"original:",[49,935,68],{"class":67},[49,937,84],{"class":63},[49,939,940],{"class":112}," original",[49,942,335],{"class":63},[49,944,945,947,949,951,954,956,958,961],{"class":51,"line":316},[49,946,123],{"class":122},[49,948,126],{"class":63},[49,950,68],{"class":67},[49,952,953],{"class":71},"other_ref:",[49,955,68],{"class":67},[49,957,84],{"class":63},[49,959,960],{"class":112}," other_ref",[49,962,335],{"class":63},[49,964,965,967,969,971,975,977],{"class":51,"line":338},[49,966,123],{"class":122},[49,968,126],{"class":63},[49,970,865],{"class":112},[49,972,974],{"class":973},"sVHd0","is",[49,976,960],{"class":112},[49,978,335],{"class":63},[20,980,573],{},[40,982,984],{"className":42,"code":983,"language":44,"meta":45,"style":45},"original: {}\nother_ref: {}\nTrue\n",[15,985,986,994,1003],{"__ignoreMap":45},[49,987,988,990,992],{"class":51,"line":52},[49,989,912],{"class":55},[49,991,77],{"class":63},[49,993,450],{"class":63},[49,995,996,999,1001],{"class":51,"line":103},[49,997,998],{"class":55},"other_ref",[49,1000,77],{"class":63},[49,1002,450],{"class":63},[49,1004,1005],{"class":51,"line":119},[49,1006,1007],{"class":583},"True\n",[20,1009,1010],{},"This shows:",[155,1012,1013,1021,1026],{},[158,1014,1015,1017,1018,1020],{},[15,1016,912],{}," and ",[15,1019,998],{}," refer to the same dictionary",[158,1022,1023,1025],{},[15,1024,17],{}," changes that dictionary in place",[158,1027,1028],{},"Both variables now show an empty dictionary",[20,1030,1031],{},"Now compare that with reassignment:",[40,1033,1035],{"className":42,"code":1034,"language":44,"meta":45,"style":45},"original = {\"a\": 1, \"b\": 2}\nother_ref = original\n\noriginal = {}\n\nprint(\"original:\", original)\nprint(\"other_ref:\", other_ref)\nprint(original is other_ref)\n",[15,1036,1037,1069,1077,1081,1089,1093,1111,1129],{"__ignoreMap":45},[49,1038,1039,1041,1043,1045,1047,1049,1051,1053,1055,1057,1059,1061,1063,1065,1067],{"class":51,"line":52},[49,1040,865],{"class":55},[49,1042,60],{"class":59},[49,1044,64],{"class":63},[49,1046,68],{"class":67},[49,1048,72],{"class":71},[49,1050,68],{"class":67},[49,1052,77],{"class":63},[49,1054,81],{"class":80},[49,1056,84],{"class":63},[49,1058,87],{"class":67},[49,1060,90],{"class":71},[49,1062,68],{"class":67},[49,1064,77],{"class":63},[49,1066,97],{"class":80},[49,1068,100],{"class":63},[49,1070,1071,1073,1075],{"class":51,"line":103},[49,1072,898],{"class":55},[49,1074,60],{"class":59},[49,1076,903],{"class":55},[49,1078,1079],{"class":51,"line":119},[49,1080,313],{"emptyLinePlaceholder":312},[49,1082,1083,1085,1087],{"class":51,"line":284},[49,1084,865],{"class":55},[49,1086,60],{"class":59},[49,1088,450],{"class":63},[49,1090,1091],{"class":51,"line":304},[49,1092,313],{"emptyLinePlaceholder":312},[49,1094,1095,1097,1099,1101,1103,1105,1107,1109],{"class":51,"line":309},[49,1096,123],{"class":122},[49,1098,126],{"class":63},[49,1100,68],{"class":67},[49,1102,933],{"class":71},[49,1104,68],{"class":67},[49,1106,84],{"class":63},[49,1108,940],{"class":112},[49,1110,335],{"class":63},[49,1112,1113,1115,1117,1119,1121,1123,1125,1127],{"class":51,"line":316},[49,1114,123],{"class":122},[49,1116,126],{"class":63},[49,1118,68],{"class":67},[49,1120,953],{"class":71},[49,1122,68],{"class":67},[49,1124,84],{"class":63},[49,1126,960],{"class":112},[49,1128,335],{"class":63},[49,1130,1131,1133,1135,1137,1139,1141],{"class":51,"line":338},[49,1132,123],{"class":122},[49,1134,126],{"class":63},[49,1136,865],{"class":112},[49,1138,974],{"class":973},[49,1140,960],{"class":112},[49,1142,335],{"class":63},[20,1144,573],{},[40,1146,1148],{"className":42,"code":1147,"language":44,"meta":45,"style":45},"original: {}\nother_ref: {'a': 1, 'b': 2}\nFalse\n",[15,1149,1150,1158,1190],{"__ignoreMap":45},[49,1151,1152,1154,1156],{"class":51,"line":52},[49,1153,912],{"class":55},[49,1155,77],{"class":63},[49,1157,450],{"class":63},[49,1159,1160,1162,1164,1166,1168,1170,1172,1174,1176,1178,1180,1182,1184,1186,1188],{"class":51,"line":103},[49,1161,998],{"class":55},[49,1163,77],{"class":63},[49,1165,64],{"class":63},[49,1167,397],{"class":67},[49,1169,72],{"class":71},[49,1171,397],{"class":67},[49,1173,77],{"class":63},[49,1175,81],{"class":80},[49,1177,84],{"class":63},[49,1179,406],{"class":67},[49,1181,90],{"class":71},[49,1183,397],{"class":67},[49,1185,77],{"class":63},[49,1187,97],{"class":80},[49,1189,100],{"class":63},[49,1191,1192],{"class":51,"line":119},[49,1193,1194],{"class":583},"False\n",[20,1196,1197],{},"Here:",[155,1199,1200,1206],{},[158,1201,1202,1205],{},[15,1203,1204],{},"original = {}"," creates a new dictionary",[158,1207,1208,1210],{},[15,1209,998],{}," still points to the old one",[20,1212,1213,1214,109],{},"If you want to make a separate dictionary before changing it, see ",[72,1215,1217],{"href":1216},"\u002Freference\u002Fpython-dictionary-copy-method\u002F",[15,1218,1219],{},"dict.copy()",[35,1221,1223,1224],{"id":1222},"when-to-use-clear","When to use ",[15,1225,17],{},[20,1227,1228,1230],{},[15,1229,17],{}," is useful when you want to:",[155,1232,1233,1236,1239],{},[158,1234,1235],{},"Reset stored data before reusing a dictionary",[158,1237,1238],{},"Empty a cache or temporary mapping",[158,1240,1241],{},"Remove all items without deleting the variable",[20,1243,1244],{},"Example:",[40,1246,1248],{"className":42,"code":1247,"language":44,"meta":45,"style":45},"session_data = {\n    \"user_id\": 101,\n    \"theme\": \"dark\",\n    \"logged_in\": True\n}\n\n# Reset for a new session\nsession_data.clear()\n\nprint(session_data)  # {}\n",[15,1249,1250,1259,1275,1295,1309,1313,1317,1322,1333,1337],{"__ignoreMap":45},[49,1251,1252,1255,1257],{"class":51,"line":52},[49,1253,1254],{"class":55},"session_data ",[49,1256,60],{"class":59},[49,1258,243],{"class":63},[49,1260,1261,1263,1266,1268,1270,1273],{"class":51,"line":103},[49,1262,248],{"class":67},[49,1264,1265],{"class":71},"user_id",[49,1267,68],{"class":67},[49,1269,77],{"class":63},[49,1271,1272],{"class":80}," 101",[49,1274,265],{"class":63},[49,1276,1277,1279,1282,1284,1286,1288,1291,1293],{"class":51,"line":119},[49,1278,248],{"class":67},[49,1280,1281],{"class":71},"theme",[49,1283,68],{"class":67},[49,1285,77],{"class":63},[49,1287,87],{"class":67},[49,1289,1290],{"class":71},"dark",[49,1292,68],{"class":67},[49,1294,265],{"class":63},[49,1296,1297,1299,1302,1304,1306],{"class":51,"line":284},[49,1298,248],{"class":67},[49,1300,1301],{"class":71},"logged_in",[49,1303,68],{"class":67},[49,1305,77],{"class":63},[49,1307,1308],{"class":583}," True\n",[49,1310,1311],{"class":51,"line":304},[49,1312,100],{"class":63},[49,1314,1315],{"class":51,"line":309},[49,1316,313],{"emptyLinePlaceholder":312},[49,1318,1319],{"class":51,"line":316},[49,1320,1321],{"class":134},"# Reset for a new session\n",[49,1323,1324,1327,1329,1331],{"class":51,"line":338},[49,1325,1326],{"class":55},"session_data",[49,1328,109],{"class":63},[49,1330,113],{"class":112},[49,1332,116],{"class":63},[49,1334,1335],{"class":51,"line":343},[49,1336,313],{"emptyLinePlaceholder":312},[49,1338,1339,1341,1343,1345,1347],{"class":51,"line":355},[49,1340,123],{"class":122},[49,1342,126],{"class":63},[49,1344,1326],{"class":112},[49,1346,131],{"class":63},[49,1348,135],{"class":134},[20,1350,1351,1352,1358,1359,109,1363,1366,1367,109],{},"If you only want to remove one item, use a more specific method like ",[72,1353,1355],{"href":1354},"\u002Freference\u002Fpython-dictionary-pop-method\u002F",[15,1356,1357],{},"dict.pop()"," or read ",[72,1360,1362],{"href":1361},"\u002Fhow-to\u002Fhow-to-remove-a-key-from-a-dictionary-in-python\u002F","how to remove a key from a dictionary in Python",[1364,1365],"br",{},"\nIf you want to remove the last inserted item, see ",[72,1368,1370],{"href":1369},"\u002Freference\u002Fpython-dictionary-popitem-method\u002F",[15,1371,1372],{},"dict.popitem()",[35,1374,1376],{"id":1375},"common-mistakes","Common mistakes",[20,1378,1379,1380,77],{},"Here are the most common problems beginners run into with ",[15,1381,17],{},[155,1383,1384,1390,1396,1399],{},[158,1385,1386,1387,1389],{},"Assigning the result of ",[15,1388,17],{}," to a variable",[158,1391,1392,1393,1395],{},"Expecting ",[15,1394,17],{}," to return a new dictionary",[158,1397,1398],{},"Not realizing the dictionary is changed in place",[158,1400,1401],{},"Clearing a dictionary that is shared by another variable",[20,1403,1404],{},"If something looks wrong, these quick checks can help:",[40,1406,1408],{"className":42,"code":1407,"language":44,"meta":45,"style":45},"print(my_dict)\nprint(type(my_dict))\nprint(id(my_dict))\nprint(other_ref is my_dict)\n",[15,1409,1410,1420,1437,1452],{"__ignoreMap":45},[49,1411,1412,1414,1416,1418],{"class":51,"line":52},[49,1413,123],{"class":122},[49,1415,126],{"class":63},[49,1417,199],{"class":112},[49,1419,335],{"class":63},[49,1421,1422,1424,1426,1430,1432,1434],{"class":51,"line":103},[49,1423,123],{"class":122},[49,1425,126],{"class":63},[49,1427,1429],{"class":1428},"sZMiF","type",[49,1431,126],{"class":63},[49,1433,199],{"class":112},[49,1435,1436],{"class":63},"))\n",[49,1438,1439,1441,1443,1446,1448,1450],{"class":51,"line":119},[49,1440,123],{"class":122},[49,1442,126],{"class":63},[49,1444,1445],{"class":122},"id",[49,1447,126],{"class":63},[49,1449,199],{"class":112},[49,1451,1436],{"class":63},[49,1453,1454,1456,1458,1460,1462,1464],{"class":51,"line":284},[49,1455,123],{"class":122},[49,1457,126],{"class":63},[49,1459,898],{"class":112},[49,1461,974],{"class":973},[49,1463,539],{"class":112},[49,1465,335],{"class":63},[20,1467,1468],{},"What these show:",[155,1470,1471,1477,1483,1489],{},[158,1472,1473,1476],{},[15,1474,1475],{},"print(my_dict)"," shows the current contents",[158,1478,1479,1482],{},[15,1480,1481],{},"print(type(my_dict))"," confirms it is still a dictionary",[158,1484,1485,1488],{},[15,1486,1487],{},"print(id(my_dict))"," helps you check whether it is the same object as before",[158,1490,1491,1494],{},[15,1492,1493],{},"print(other_ref is my_dict)"," tells you whether two variables point to the same object",[35,1496,1498],{"id":1497},"faq","FAQ",[591,1500,1502,1503,1505],{"id":1501},"does-clear-delete-the-dictionary","Does ",[15,1504,17],{}," delete the dictionary?",[20,1507,1508],{},"No. It keeps the dictionary object but removes all items from it.",[591,1510,1512,1513,1515],{"id":1511},"what-does-dictclear-return","What does ",[15,1514,24],{}," return?",[20,1517,1518,1519,109],{},"It returns ",[15,1520,169],{},[591,1522,1524,1525,1017,1527,1529],{"id":1523},"what-is-the-difference-between-clear-and","What is the difference between ",[15,1526,17],{},[15,1528,175],{},"?",[20,1531,1532,1534,1535,1537],{},[15,1533,17],{}," empties the same dictionary object. Assigning ",[15,1536,175],{}," creates a new empty dictionary.",[591,1539,1502,1541,1543],{"id":1540},"does-clear-work-on-nested-dictionaries",[15,1542,17],{}," work on nested dictionaries?",[20,1545,1546],{},"It removes the top-level items. If nested dictionaries were inside it, those references are removed from this dictionary.",[35,1548,1550],{"id":1549},"see-also","See also",[155,1552,1553,1557,1563,1572,1579,1586,1591],{},[158,1554,1555],{},[72,1556,185],{"href":184},[158,1558,1559],{},[72,1560,1562],{"href":1561},"\u002Freference\u002Fpython-dictionary-creating-a-dictionary\u002F","Creating a dictionary in Python",[158,1564,1565],{},[72,1566,1567,1568,1571],{"href":1354},"Python dictionary ",[15,1569,1570],{},"pop()"," method",[158,1573,1574],{},[72,1575,1567,1576,1571],{"href":1369},[15,1577,1578],{},"popitem()",[158,1580,1581],{},[72,1582,1567,1583,1571],{"href":1216},[15,1584,1585],{},"copy()",[158,1587,1588],{},[72,1589,1590],{"href":844},"Mutable vs immutable types in Python",[158,1592,1593],{},[72,1594,1595],{"href":1361},"How to remove a key from a dictionary in Python",[1597,1598,1599],"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 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 .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 .s39Yj, html code.shiki .s39Yj{--shiki-light:#39ADB5;--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 .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":45,"searchDepth":103,"depth":103,"links":1601},[1602,1603,1605,1606,1607,1611,1616,1617,1619,1620,1630],{"id":37,"depth":103,"text":38},{"id":143,"depth":103,"text":1604},"What clear() does",{"id":188,"depth":103,"text":189},{"id":224,"depth":103,"text":225},{"id":472,"depth":103,"text":1608,"children":1609},"What clear() returns",[1610],{"id":593,"depth":119,"text":594},{"id":774,"depth":103,"text":1612,"children":1613},"clear() vs creating a new empty dictionary",[1614,1615],{"id":113,"depth":119,"text":17},{"id":199,"depth":119,"text":827},{"id":848,"depth":103,"text":849},{"id":1222,"depth":103,"text":1618},"When to use clear()",{"id":1375,"depth":103,"text":1376},{"id":1497,"depth":103,"text":1498,"children":1621},[1622,1624,1626,1628],{"id":1501,"depth":119,"text":1623},"Does clear() delete the dictionary?",{"id":1511,"depth":119,"text":1625},"What does dict.clear() return?",{"id":1523,"depth":119,"text":1627},"What is the difference between clear() and {}?",{"id":1540,"depth":119,"text":1629},"Does clear() work on nested dictionaries?",{"id":1549,"depth":103,"text":1550},"Master python dictionary clear method in our comprehensive Python beginner guide.","md",{},"\u002Freference\u002Fpython-dictionary-clear-method",{"title":5,"description":1631},"reference\u002Fpython-dictionary-clear-method","xe2WHgVVqBBDcVsY_J76LhYda8_TxsqdNPbHatiN8IU",1777585513868]