Python dictionary with keys having multiple inputs

PythonServer Side ProgrammingProgramming

When it is required to create a dictionary where keys have multiple inputs, an empty dictionary can be created, and the value for a specific key can be specified.

Below is the demonstration of the same −

Example

 Live Demo

my_dict = {}

a, b, c = 15, 26, 38
my_dict[a, b, c] = a + b - c

a, b, c = 5, 4, 11
my_dict[a, b, c] = a + b - c

print("The dictionary is :")
print(my_dict)

Output

The dictionary is :
{(15, 26, 38): 3, (5, 4, 11): -2}

Explanation

  • The required packages are imported.

  • An empty dictionary is defined.

  • The values for a specific key are specified.

  • The output is displayed on the console.

raja
Published on 17-Apr-2021 16:14:56
Advertisements