Python Dictionary with Keys having Multiple Inputs (original) (raw)

Last Updated : 3 Feb, 2026

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:

Python `

points = {}

points[(1, 2)] = "A" points[(3, 4)] = "B"

print(points[(1, 2)]) print(points[(3, 4)])

`

**Explanation:

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.

Python `

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:

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.

Python `

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:

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.

Python `

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"])

`