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 a single key. So in this article we will discuss how we can map multiple values per key in dictionary.
Syntax of dictionary :
dictionary_name = {key1: value1, key2: value2}
where,
- key1, key2… represents keys in a dictionary. These keys can be a string, integer, tuple, etc. But keys needs to be unique.
- value1, value2… represents values in dictionary. These values can be numbers, strings, list or list within a list etc.
- key and value is separated by : symbol.
- key-value pair symbol is separated by , symbol.
Example of a dictionary covid_case
where multiple values are associated with single key.
covid_case = {"January": [1000,1500,2000], "February": [1100,1300,1500], "March":[2000,2200,2400] }
So, now let’s explore the concept how to add a key:value
 pair to dictionary in Python.
Create a dictionary with a list as the value :
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.
#Program : #Multiple values mapped to single key covid_case = {"January": [1000,1500,2000], "February": [1100,1300,1500], "March":[2000,2200,2400] } #printing the multidict print("Dictionary with multiple values per key :") print(covid_case)
Output : Dictionary with multiple values per key : {"January": [1000,1500,2000], "February": [1100,1300,1500], "March":[2000,2200,2400] }
In this example string keys have taken in a dictionary where multiple integer values are mapped to single key.
Finding multiple values of a key in the dictionary :
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.
#Program : #Multiple values mapped to single key covid_case = {"January": [1000,1500,2000], "February": [1100,1300,1500], "March":[2000,2200,2400] } #printing the multidict print("Complete dictionary :") print(covid_case) #Getting the list(multiple values) at key February output_list=covid_case["February"] #printing the multiple values of key February print("Multiple values of key 'February' :") print(output_list)
Output : Complete dictionary : {"January": [1000,1500,2000], "February": [1100,1300,1500], "March":[2000,2200,2400] } Multiple values of key 'February' : [1100,1300,1500]
Append multiple values to a key in a dictionary :
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.
So there 2 cases may arise i.e
- The key is already present with multiple values and new values will be added in that key.
- The key is not present in the dictionary, so new key with multiple values will be appended to that dictionary.
CASE -1 : Key is already present in dictionary :
#Program : #Multiple values mapped to single key covid_case = {"January": [1000,1500,2000], "February": [1100,1300,1500], "March":[2000,2200,2400] } #printing the multidict print("Before append :") print(covid_case) # Append multiple values to a key in dictionary covid_case def add_values_to_key(temp_dict, key, list_of_values): if key not in temp_dict: temp_dict[key] = list() temp_dict[key].extend(list_of_values) return temp_dict # Append multiple values for existing key 'March' covid_case = add_values_to_key(covid_case, 'March', [2600, 2800]) #printing the multidict print("After append :") print(covid_case)
Output : Before append : {"January": [1000,1500,2000], "February": [1100,1300,1500], "March":[2000,2200,2400] } After append : {"January": [1000,1500,2000], "February": [1100,1300,1500], "March":[2000,2200,2400,2600,2800] }
CASE -2 : Key is not present in dictionary :
#Program : #Multiple values mapped to single key covid_case = {"January": [1000,1500,2000], "February": [1100,1300,1500], "March":[2000,2200,2400] } #printing the multidict print("Before append :") print(covid_case) # Append multiple values to a key in dictionary covid_case def add_values_to_key(temp_dict, key, list_of_values): if key not in temp_dict: temp_dict[key] = list() temp_dict[key].extend(list_of_values) return temp_dict # Append multiple values for key 'April' #As key 'April' is not present so new key with multiple values will be added covid_case = add_values_to_key(covid_case, 'April', [3000, 3500]) #printing the multidict print("after append :") print(covid_case)
Output : Before append : {"January": [1000,1500,2000], "February": [1100,1300,1500], "March":[2000,2200,2400] } After append : {"January": [1000,1500,2000], "February": [1100,1300,1500], "March":[2000,2200,2400], "April":[3000,3500 }