In Python, dictionaries store data as key-value pairs. Tuples can be used as keys to represent multiple values, like coordinates or combinations, because they are immutable and hashable.
Let's consider an example where tuples are used as keys to store multiple numbers together:
points = {}
points[(1, 2)] = "A"
points[(3, 4)] = "B"
print(points[(1, 2)])
print(points[(3, 4)])
Output
A B
Explanation:
- Each key is a tuple representing multiple inputs.
- The value can be any data (here, "A" and "B").
- Access values using the tuple key.
Dictionary with Multiple Inputs as Keys
Let's consider an example where we use tuples as dictionary keys to store multiple numbers together, with each tuple mapping to a value.
dict = {}
dict[(1, 2, 3)] = "First"
dict[(4, 5, 6)] = "Second"
print(dict[(1, 2, 3)])
print(dict[(4, 5, 6)])
print(dict)
Output
First
Second
{(1, 2, 3): 'First', (4, 5, 6): 'Second'}
Explanation:
- dict[(1, 2, 3)] = "First": Uses the tuple (1, 2, 3) as a key and stores "First" as its value.
- dict[(4, 5, 6)] = "Second": Uses the tuple (4, 5, 6) as a key and stores "Second" as its value.
- You can access values using the tuple key: dict[(1, 2, 3)] gives "First".
Using Multi-Keys to Store Coordinates
Let's consider a scenario where latitude and longitude are used as keys, and their corresponding place names are stored as values.
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])
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']
Explanation:
- Tuples (latitude, longitude) are used as dictionary keys.
- Values store the place names.
- You can easily extract latitudes, longitudes, and place names into separate lists.
Complex Dictionary with Multiple Key Inputs
In this example, each key is a tuple containing three elements - an integer ID, first name, and last name, and each value is itself a nested dictionary with additional details.
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'}
Explanation:
- Each tuple key uniquely identifies a record.
- The nested dictionary stores related data for that key.
- You can easily access or update values using the key tuple and dictionary keys.