Signup/Sign In

Python dictionary with keys having multiple inputs

In this tutorial, you will learn to create a dictionary with keys having multiple inputs in Python. We have to create a dictionary, in which there will be one key for multiple inputs.

Take a look at the sample input-output of the program.

Input: x= 2 y= 1 z=9 , p=3, p=5, r=2

Output: {'x+y+z' : [12, 10]}

Input: {''words with d" : ['dog', 'dark', 'dance'] , "words with a": ['apple', 'act', 'ace']}

Output:{''words with d" : ['dog', 'dark', 'dance', 'den'] , "words with a": ['apple', 'act', 'ace','all']}

To solve this problem, we will first create an empty dictionary and insert multiple values in it. We will see two examples of when we have to create a dictionary with keys having multiple inputs.

Example 1: linear equation as key

For this let us consider an example where we have a linear equation of three vari\ables x, y, and z. We have to store the values given by the equation for different values of x,y, and z given as input.

Algorithm

Follow the algorithm to understand the approach better.

Step 1- Define an empty dictionary

Step 2- Insert values of x, y and z

Step 3- In the dictionary store x,y and z as keys and the value given by the equation will be stored as the value

Step 4- Give multiple values for the x, y and z keys

Step 5- Print the dictionary

Python Program 1

Look at the Python code to understand the implementation. We have created a dictionary that will have the value of x,y and z as keys and the value given by the equation will be the value in the dictionary.

# creating an empty dictionary
dic = {}
# Inserting first inputs in dictionary
a,b,c= 5, 3, 10
# Insert second inputs in dictionary
p,q,r= 12, 6, 9
dic["x-y+z"] = [a-b+c,p-q+r]
# print the dictionary
print(dic)


{'x-y+z': [12, 15]}

Example 2: States as keys

In this example, we will have to create a dictionary where the state name is the key and the different cities in the state are its values.

Algorithm

Follow the algorithm to understand the approach better.

Step 1- Define a function to add values in the key

Step 2- In the function check if the key is not in the dictionary we will create a new key in the dictionary

Step 3- Else add the value in the key

Step 4- Return the dictionary after adding values

Step 5- Create a dictionary with states as keys and cities as values

Step 6- Print original Dictionary

Step 7- Declare another variable to store the value returned by the function

Step 8- Call the defined function to add value in the key

Step 9- Print the value in the variable

Python Program 2

Look at the Python code to understand the implementation. In this program, we have defined a dictionary for storing the multiple cities of a state.

# dictionary with multiple inputs in a key

# function to add multiple values
def add_values_in_dict(dic, key, list_of_values):
    """Append multiple values to a key in the given dictionary"""
    if key not in dic:
        dic[key] = list()
    dic[key].extend(list_of_values)
    return dic
 
# dictionary containing states and its cities
places = {("Maharashtra"):["Mumbai","Pune","Nagpur"],
          ("Madhya Pradesh"):["Delhi","Bhopal","Indore"]}
 
print(places)
print('\n')

#adding values
add_val=add_values_in_dict(places, "Madhya Pradesh", ["Ujjain", "Sagar"])
print("After adding values")
print(add_val)


{'Maharashtra': ['Mumbai', 'Pune', 'Nagpur'], 'Madhya Pradesh': ['Delhi', 'Bhopal', 'Indore']}

After adding values
{'Maharashtra': ['Mumbai', 'Pune', 'Nagpur'], 'Madhya Pradesh': ['Delhi', 'Bhopal', 'Indore', 'Ujjain', 'Sagar']}

Conclusion

So far we have learned how to create dictionaries with keys having multiple inputs and also we have seen the different examples where this can be used. We have used a list, for storing multiple values in a single key of the dictionary.