Python Program to Add a Key, Value Pair to the Dictionary

The best and excellent way to learn a java programming language is by practicing Simple Java Program Examples as it includes basic to advanced levels of concepts.

Dictionaries in Python:

In Python, a dictionary dict is a one-to-one mapping; it includes a set of (key, value) pairs, with each key mapped to a value. It exemplifies a hash map or hash table (from Computer Science).

Each key denotes a value and is separated by a colon (:).

Curly brackets are used to define a dictionary. The value to the left of the colon is known as the key, while the value to the right of the colon is known as the value. A comma separates each (key, value) pair.

Example:

Example1:

Input:

Enter some random key = aplustopper
Enter some random value of integer type = 2451

Output:

The dictionary after adding the key = aplustopper and value element = 2451 is : 
{'aplustopper': 2451}

Example2:

Input:

Enter some random key = 752
Enter some random value of integer type = 251

Output:

The dictionary after adding the key = 752 and value element = 251 is : 
{752: 251}

Example3:

Input:

Enter some random key = 234
Enter some random value of string type = btechgeeks

Output:

The dictionary after adding the key = 234 and value element = btechgeeks is : 
{234: 'btechgeeks'}

Program to Add a Key , Value Pair to the Dictionary

There are several ways to add a key, value pair to the dictionary some of them are:

Method #1:Using update() function (User input)

Approach:

  • Give the key , value as user input and store them in two separate variables.
  • Declare a dictionary and set its initial value to empty using {} or dict().
  • To add the key-value pair to the dictionary, use the update() function.
  • The Modified dictionary is printed.
  • Exit of program.

i)Key of string datatype and value as integer datatype

Below is the implementation:

# given some random key
keyelement = input("Enter some random key = ")
# given some random value of int datatype
valueelement = int(input("Enter some random value of integer type = "))
# Declare a dictionary and set its initial value to empty using {} or dict()
sampledictionary = {}
# To add the key-value pair to the dictionary, use the update() function.
sampledictionary.update({keyelement: valueelement})
# The Modified dictionary is printed
print("The dictionary after adding the key =", keyelement,
      "and value element =", valueelement, " is : ")
print(sampledictionary)

Output:

Enter some random key = btechgeeks
Enter some random value of integer type = 2841
The dictionary after adding the key = btechgeeks and value element = 2841 is : 
{'btechgeeks': 2841}

ii)Key of integer datatype and value as integer datatype

# given some random key
keyelement = int(input("Enter some random key = "))
# given some random value of int datatype
valueelement = int(input("Enter some random value of integer type = "))
# Declare a dictionary and set its initial value to empty using {} or dict()
sampledictionary = {}
# To add the key-value pair to the dictionary, use the update() function.
sampledictionary.update({keyelement: valueelement})
# The Modified dictionary is printed
print("The dictionary after adding the key =", keyelement,
      "and value element =", valueelement, " is : ")
print(sampledictionary)

Output:

Enter some random key = 752
Enter some random value of integer type = 251
The dictionary after adding the key = 752 and value element = 251 is : 
{752: 251}

iii)Key of integer datatype and value as string datatype

# given some random key
keyelement = int(input("Enter some random key = "))
# given some random value of string datatype
valueelement = input("Enter some random value of string type = ")
# Declare a dictionary and set its initial value to empty using {} or dict()
sampledictionary = {}
# To add the key-value pair to the dictionary, use the update() function.
sampledictionary.update({keyelement: valueelement})
# The Modified dictionary is printed
print("The dictionary after adding the key =", keyelement,
      "and value element =", valueelement, " is : ")
print(sampledictionary)

Output:

Enter some random key = 234
Enter some random value of string type = btechgeeks
The dictionary after adding the key = 234 and value element = btechgeeks is : 
{234: 'btechgeeks'}

Method #2:Using [] operator(User input)

Below is the implementation:

# given some random key
keyelement = input("Enter some random key = ")
# given some random value of integer datatype
valueelement = int(input("Enter some random value of integer type = "))
# Declare a dictionary and set its initial value to empty using {} or dict()
sampledictionary = {}
# using [] function
sampledictionary[keyelement] = valueelement
# The Modified dictionary is printed
print("The dictionary after adding the key =", keyelement,
      "and value element =", valueelement, " is : ")
print(sampledictionary)

Output:

Enter some random key = aplustopper
Enter some random value of integer type = 2451
The dictionary after adding the key = aplustopper and value element = 2451 is : 
{'aplustopper': 2451}

Related Programs: