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