Add key value pair to dictionary Python
In this post, you will learn different ways to add key value pairs to a dictionary in Python programming.
Dictionary is a collection whose values are accessible by key. The dictionary is a mapping of key & value. The order of elements in a dictionary is undefined. We can iterate over keys, values and key-value pairs. It is separated by a colon(:), and the key/value pair is separated by comma(,). Python has an in-built dictionary called dict, which we can use to create an arbitrary definition of the character string. The dict() is a constructor which is used to create instances of the class dict.
The general syntax of dictionary is -
{key1: value1, key2: value2, key3: value3, ....}
Using update() method
In the given example, we have used the update() method to add a new key value pair to the dictionary in Python.
# Adding single key value pair
fruitsDict = {"Apple": 10, "Banana" : 32 , "Plum" : 15, "Mango" : 31}
print(fruitsDict)
fruitsDict.update( {'Papaya' : 12} )
print(fruitsDict)
# Adding multiple key value pairs
fruitsDict.update( [('Kiwi', 12),('Pear',17)] )
print(fruitsDict)
Output of the above code -
{'Apple': 10, 'Banana': 32, 'Plum': 15, 'Mango': 31}
{'Apple': 10, 'Banana': 32, 'Plum': 15, 'Mango': 31, 'Papaya': 12}
{'Apple': 10, 'Banana': 32, 'Plum': 15, 'Mango': 31, 'Papaya': 12, 'Kiwi': 12, 'Pear': 17}
Using Subscript notation
The subscript operator is defined as square brackets []. It is used to access or update the elements of string, list, tuple, and so on. Here, we have used this for adding dictionary data. For this, we only need to provide the dictionary name followed by square brackets with the key name and assign a value to it, like -
# Adding single key value pair
fruitsDict = {"Apple": 10, "Banana" : 32 , "Plum" : 15, "Mango" : 31}
print(fruitsDict)
# Adding multiple key value pairs
fruitsDict['Papaya'] = 19
fruitsDict['Kiwi'] = 12
fruitsDict['Pear'] = 17
print(fruitsDict)
Output of the above code -
{'Apple': 10, 'Banana': 32, 'Plum': 15, 'Mango': 31}
{'Apple': 10, 'Banana': 32, 'Plum': 15, 'Mango': 31, 'Papaya': 19, 'Kiwi': 12, 'Pear': 17}
Getting key, value in user input and add
In the given example, we have asked the user to enter the key, value using the input() method. Next, we have stored them in two variables and used the subscript notation process to add the key value on the existing dictionary.
fruitsDict = {"Apple": 10, "Banana" : 32 , "Plum" : 15, "Mango" : 31}
print(fruitsDict)
# Getting key value from user
key = input("Enter the key: ")
value = input("Enter the value: ")
fruitsDict[key] = value
print(fruitsDict)
Output of the above code -
{'Apple': 10, 'Banana': 32, 'Plum': 15, 'Mango': 31}
Enter the key: Papaya
Enter the value: 20
{'Apple': 10, 'Banana': 32, 'Plum': 15, 'Mango': 31, 'Papaya': '20'}
Related Articles
Convert list to dictionary Python
Python dict inside list
Convert string to list Python
Convert Python list to numpy array
Python add list to list
Python loop through list
Python compare two lists
Remove element from list Python
Python iterate list with index
Python program to sum all the numbers in a list
Python print without newline
Python iterate list with index
Python add list to list
Python convert xml to dict
Python dict inside list
Python convert dict to xml
Python raise keyword