Open In App

Python dictionary with keys having multiple inputs

Prerequisite: Python-Dictionary

How to create a dictionary where a key is formed using inputs? 



Let us consider an example where have an equation for three input variables, x, y, and z. We want to store values of equation for different input triplets.

Example 1:






# Python code to demonstrate a dictionary
# with multiple inputs in a key.
import random as rn
 
# creating an empty dictionary
dict = {}
 
# Insert first triplet in dictionary
x, y, z = 10, 20, 30
dict[x, y, z] = x + y - z;
 
# Insert second triplet in dictionary
x, y, z = 5, 2, 4
dict[x, y, z] = x + y - z;
 
# print the dictionary
print(dict)

Output
{(10, 20, 30): 0, (5, 2, 4): 3}

Time complexity: O(1) for each insertion and O(n) for printing the dictionary where n is the number of key-value pairs.
Auxiliary space: O(n) to store the dictionary.

Python dictionary with keys having multiple inputs to get access to the keys. 

Let us consider a dictionary where longitude and latitude are the keys and the place to which they belong to is the value.




# dictionary containing longitude and latitude of places
places = {("19.07'53.2", "72.54'51.0"):"Mumbai", \
          ("28.33'34.1", "77.06'16.6"):"Delhi"}
 
print(places)
print('\n')
 
# Traversing dictionary with multi-keys and creating
# different lists from it
lat = []
long = []
plc = []
for i in places:
    lat.append(i[0])
    long.append(i[1])
    plc.append(places[i[0], i[1]])
 
print(lat)
print(long)
print(plc)

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']

Time complexity: O(n), where n is the number of keys in the dictionary.
Auxiliary space: O(n), where n is the number of keys in the dictionary .

Now that the keys(latitude, longitude) and values(place) are stored in a list, we can access it easily.

Python dictionary with keys having multiple inputs

The idea behind this code is to create a dictionary with multiple inputs for keys. In this case, the keys are tuples with three elements: an integer, a first name, and a last name. The values associated with each key are themselves dictionaries with three key-value pairs.

The code creates the dictionary and then accesses the values associated with certain keys using indexing. It also demonstrates how to modify the values associated with certain keys using the same indexing notation.

Step-by-step approach to implementing this code:




# Creating a dictionary with multiple inputs for keys
data = {
    (1, "John", "Doe"): {"a": "geeks", "b": "software", "c": 75000},
    (2, "Jane", "Smith"): {"e": 30, "f": "for", "g": 90000},
    (3, "Bob", "Johnson"): {"h": 35, "i": "project", "j": "geeks"},
    (4, "Alice", "Lee"): {"k": 40, "l": "marketing", "m": 100000}
}
 
# Accessing the values using the keys
print(data[(1, "John", "Doe")]["a"])
print(data[(2, "Jane", "Smith")]["f"])
print(data[(3, "Bob", "Johnson")]["j"])
 
data[(1, "John", "Doe")]["a"] = {"b": "marketing", "c": 75000};
data[(3, "Bob", "Johnson")]["j"] = {"h": 35, "i": "project"};
print(data[(1, "John", "Doe")]["a"]);
print(data[(3, "Bob", "Johnson")]["j"]);

Output
geeks
for
geeks
{'b': 'marketing', 'c': 75000}
{'h': 35, 'i': 'project'}

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.
Auxiliary space: O(n), where each key and value takes up a constant amount of memory, the space complexity of the data dictionary is O(1) for the keys. 


Article Tags :