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