Python program to Dictionary with Keys Having Multiple InputsIn this tutorial, we will write the program for that Python dictionary having multiple inputs. In the below example, we have an equation of the three input variables x, y, and z and we want to store value of the equation for different input triplets. We can represent each input triplet as a tuple (x, y, z) and store the corresponding output value of the equation in a dictionary. Here's an example of how we can implement this in Python: Example - Output: {(1, 2, 3): 7, (4, 5, 6): 29, (7, 8, 9): 79} Explanation - In this example, we defined the equation as x + y * z. We created an empty dictionary values_dict to store the values of the equation for different input triplets. We then calculated the value of the equation for three different input triplets and stored the output value in the dictionary using the input triplet as the key. Finally, we printed the dictionary to show the stored values. The time-complexity of dictionary operations in Python is remunerating O(1) for average cases, O(n) for worst-case scenarios, when n is the number of elements in the dictionary. Example - 2: Let's say we have a dictionary where the keys are longitude and latitude coordinates, and the values are the names of places associated with those coordinates. We can represent each longitude and latitude coordinate as a tuple (longitude, latitude) and store the corresponding place name in the dictionary. Example - Output: {("19.07'53.2", "72.54'51.0"): 'Mumbai', ("28.33'34.1", "77.06'16.6"): 'Delhi'} ["19.07'53.2", "28.33'34.1"] ["72.54'51.0", "77.06'16.6"] ['Mumbai', 'Delhi'] Explanation - This code defines dictionary coordinates with keys as tuples representing longitude and latitude coordinates and values as place names. The code then traverses the dictionary, extracts the longitude and latitude values from the keys, and the corresponding place name from the dictionary values, and stores them in separate lists. Example - 3: In this example, the tuples have three elements - an integer, a first name, and a last name - and each nested dictionary has three key-value pairs. The values associated with each key are themselves dictionaries with three key-value pairs. In the code, we create dictionary and then accesses the value associated with certain keys using indexing. Example - Output: javatpoint for javatpoint {'b': 'marketing', 'c': 75000} javatpoint Time complexity - O(1), where the hash function used to map the key to a bucket in the dictionary allows for constant-time access in most cases. Next TopicPython Selective Keys Summation |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India