{"id":2991,"date":"2023-10-19T19:52:37","date_gmt":"2023-10-19T14:22:37","guid":{"rendered":"https:\/\/python-programs.com\/?p=2991"},"modified":"2023-11-10T11:48:43","modified_gmt":"2023-11-10T06:18:43","slug":"python-dictionary-with-multiple-values-per-key","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-dictionary-with-multiple-values-per-key\/","title":{"rendered":"Python: Dictionary with Multiple Values per Key"},"content":{"rendered":"
In python a dictionary is one of the important datatype which is used to store data values in Syntax of dictionary :<\/strong><\/p>\n where,<\/p>\n Example of a dictionary So, now let’s explore the concept how to add a\u00a0 Generally, when multiple values are mapped to a single key, then that dictionary is called as MultiDict. So, let’s see an example how in dictionary multiple values are mapped to a single key.<\/p>\n In this example string keys have taken in a dictionary where multiple integer values are mapped to single key.<\/p>\n Like how we created a dictionary with a list as a value, similar we can get a list as output also. Means it is possible to get multiple values of a key in dictionary. So, let’s see an example how we can get multiple values of a key as a list.<\/p>\n It is easy to create a dictionary where multiple values are mapped to a single key. But the tricky part is appending multiple values where already multiple values are mapped to that key.<\/p>\n So there 2 cases may arise i.e<\/p>\n CASE -1 : Key is already present in dictionary :<\/strong><\/p>\n CASE -2 : Key is not present in dictionary :<\/strong><\/p>\n How we can create a dictionary with multiple values per key in python. In python a dictionary is one of the important datatype which is used to store data values in key : value pair. Generally keys and values are mapped one-to-one. But it is not impossible that we can not map multiple values to …<\/p>\nkey : value<\/code> pair. Generally keys and values are mapped one-to-one. But it is not impossible that we can not map multiple values to a single key. So in this article we will discuss how we can map multiple values per key in dictionary.<\/p>\n
dictionary_name = {key1: value1, key2: value2}<\/pre>\n
\n
covid_case<\/code> where multiple values are associated with single key.<\/p>\n
covid_case = {\"January\": [1000,1500,2000], \"February\": [1100,1300,1500], \"March\":[2000,2200,2400] }<\/pre>\n
key:value<\/code>\u00a0pair to dictionary in Python.<\/p>\n
Create a dictionary with a list as the value :<\/h3>\n
#Program :\r\n\r\n#Multiple values mapped to single key\r\ncovid_case = {\"January\": [1000,1500,2000], \"February\": [1100,1300,1500], \"March\":[2000,2200,2400] }\r\n#printing the multidict\r\nprint(\"Dictionary with multiple values per key :\")\r\nprint(covid_case)\r\n<\/pre>\n
Output :\r\nDictionary with multiple values per key :\r\n{\"January\": [1000,1500,2000], \"February\": [1100,1300,1500], \"March\":[2000,2200,2400] }<\/pre>\n
Finding multiple values of a key in the dictionary :<\/h3>\n
#Program :\r\n\r\n#Multiple values mapped to single key\r\ncovid_case = {\"January\": [1000,1500,2000], \"February\": [1100,1300,1500], \"March\":[2000,2200,2400] }\r\n#printing the multidict\r\nprint(\"Complete dictionary :\")\r\nprint(covid_case)\r\n#Getting the list(multiple values) at key February\r\noutput_list=covid_case[\"February\"]\r\n#printing the multiple values of key February\r\nprint(\"Multiple values of key 'February' :\")\r\nprint(output_list)<\/pre>\n
Output :\r\nComplete dictionary :\r\n{\"January\": [1000,1500,2000], \"February\": [1100,1300,1500], \"March\":[2000,2200,2400] }\r\nMultiple values of key 'February' :\r\n[1100,1300,1500]<\/pre>\n
Append multiple values to a key in a dictionary :<\/h3>\n
\n
#Program :\r\n\r\n#Multiple values mapped to single key\r\ncovid_case = {\"January\": [1000,1500,2000], \"February\": [1100,1300,1500], \"March\":[2000,2200,2400] }\r\n#printing the multidict\r\nprint(\"Before append :\")\r\nprint(covid_case)\r\n\r\n\r\n# Append multiple values to a key in dictionary covid_case \r\ndef add_values_to_key(temp_dict, key, list_of_values):\r\n if key not in temp_dict:\r\n temp_dict[key] = list()\r\n temp_dict[key].extend(list_of_values)\r\n return temp_dict\r\n \r\n \r\n# Append multiple values for existing key 'March'\r\ncovid_case = add_values_to_key(covid_case, 'March', [2600, 2800])\r\n#printing the multidict\r\nprint(\"After append :\")\r\nprint(covid_case)\r\n<\/pre>\n
Output :\r\nBefore append :\r\n{\"January\": [1000,1500,2000], \"February\": [1100,1300,1500], \"March\":[2000,2200,2400] }\r\nAfter append :\r\n{\"January\": [1000,1500,2000], \"February\": [1100,1300,1500], \"March\":[2000,2200,2400,2600,2800] }<\/pre>\n
#Program :\r\n\r\n#Multiple values mapped to single key\r\ncovid_case = {\"January\": [1000,1500,2000], \"February\": [1100,1300,1500], \"March\":[2000,2200,2400] }\r\n#printing the multidict\r\nprint(\"Before append :\")\r\nprint(covid_case)\r\n\r\n\r\n# Append multiple values to a key in dictionary covid_case \r\ndef add_values_to_key(temp_dict, key, list_of_values):\r\n if key not in temp_dict:\r\n temp_dict[key] = list()\r\n temp_dict[key].extend(list_of_values)\r\n return temp_dict\r\n \r\n \r\n# Append multiple values for key 'April'\r\n#As key 'April' is not present so new key with multiple values will be added\r\ncovid_case = add_values_to_key(covid_case, 'April', [3000, 3500])\r\n#printing the multidict\r\nprint(\"after append :\")\r\nprint(covid_case)\r\n<\/pre>\n
Output :\r\nBefore append :\r\n{\"January\": [1000,1500,2000], \"February\": [1100,1300,1500], \"March\":[2000,2200,2400] }\r\nAfter append :\r\n{\"January\": [1000,1500,2000], \"February\": [1100,1300,1500], \"March\":[2000,2200,2400], \"April\":[3000,3500 }<\/pre>\n","protected":false},"excerpt":{"rendered":"