Python dictionary with keys having multiple inputs
Last Updated :
27 Jul, 2023
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:
Python3
import random as rn
dict = {}
x, y, z = 10 , 20 , 30
dict [x, y, z] = x + y - z;
x, y, z = 5 , 2 , 4
dict [x, y, z] = x + y - z;
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.
Python3
places = {( "19.07'53.2" , "72.54'51.0" ): "Mumbai" , \
( "28.33'34.1" , "77.06'16.6" ): "Delhi" }
print (places)
print ( '\n' )
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:
- Start by defining a dictionary variable named “data”.
- Inside the curly braces, define each key-value pair using a tuple as the key and a dictionary as the value. Separate each key-value pair with a comma.
- Inside each tuple key, include three elements: an integer, a first name, and a last name. Separate each element with a comma.
- Inside each dictionary value, include three key-value pairs. The keys can be any string or integer, and the values can be any data type.
- To access a value in the dictionary, use indexing with the key tuple. For example, to print the value associated with the key (1, “John”, “Doe”) and the key “a”, use the notation “data[(1, “John”, “Doe”)][“a”]”.
- To modify a value in the dictionary, use indexing with the key tuple and then assign the new value using the equals sign. For example, to change the value associated with the key (1, “John”, “Doe”) and the key “a”, use the notation “data[(1, “John”, “Doe”)][“a”] = {“b”: “marketing”, “c”: 75000}”.
- Print the modified value to verify the change.
Python3
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 }
}
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.
Please Login to comment...