The dictionary is a data type in Python that is used to store data in the form of key/value pairs. We have written a detailed tutorial about creating, adding, removing and other operations related to the Python dictionary.
In this tutorial, we will see the ways of adding one or more items (key/value pairs) in dictionaries. You will also see how to update the values of existing keys in a dictionary.
The example of Python append to dictionary
In this example, first I created a dictionary of telephone numbers with three items. Then, I used the print function to display the items in the dictionary.
After that, I added another item and displayed the dictionary again:
The code:
#Creating a dictionary tel_directory = {'David': 14587478454,'Haynes': 2811441425, 'Mina':2814555555} print (tel_directory) #Adding a new item tel_directory['Azam'] = 2814525458 print ("After adding item") print (tel_directory)
The output:
{‘Haynes’: 2811441425, ‘Mina’: 2814555555, ‘David’: 14587478454}
After adding item
{‘Haynes’: 2811441425, ‘Mina’: 2814555555, ‘David’: 14587478454, ‘Azam’: 2814525458}
You can see, a new item is added to the existing dictionary by using a simple assignment operator.
Notice this line in the above example:
So, you just need to provide the dictionary name, key in brackets and assign a value to it.
Adding multiple items in a dictionary example
For adding multiple items, just keep on using the assignment operator (=) with the key and provide value.
See the example code below where I just extended the above example and added three new items to an existing dictionary:
#Creating a dictionary tel_directory = {'David': 14587478454,'Haynes': 2811441425, 'Mina':2814555555} print (tel_directory) #Adding a new item tel_directory['Azam'] = 2814525458 tel_directory['Joe'] = 2817484758 tel_directory['Jimmy'] = 2816957458 print ("After adding item") print (tel_directory)
The result:
{‘Mina’: 2814555555, ‘David’: 14587478454, ‘Haynes’: 2811441425}
After adding item
{‘David’: 14587478454, ‘Azam’: 2814525458, ‘Mina’: 2814555555, ‘Joe’: 2817484758, ‘Haynes’: 2811441425, ‘Jimmy’: 2816957458}
You can see, the dictionary after adding new items contains six items.
What if I want to update an existing item?
For updating the value of an existing key, use the same key and provide the new value. This will update the value rather than adding a new item.
In this example, we have created a dictionary with three telephone numbers/names and then updated the number of “Haynes” as follows:
#Creating a dictionary tel_directory = {'David': 14587478454,'Haynes': 2811441425, 'Mina':2814555555} print (tel_directory) #Updating an existing item tel_directory['Haynes'] = 2811568744 print ("After Updating item") print (tel_directory)
The output of above code:
{‘Mina’: 2814555555, ‘Haynes’: 2811441425, ‘David’: 14587478454}
After updating item
{‘Mina’: 2814555555, ‘Haynes’: 2811568744, ‘David’: 14587478454}
Second way: Using the update method of dictionary
Python dictionary has a built-in method, update, that can be used for adding new items to the specified dictionary.
You may also use it to update the value of an existing key. In this case, just provide the same key that already exists in the dictionary.
An example of appending a new key/value pair
The example below shows adding a new key/value pair in a dictionary. Initially, I created a dictionary with three items, as you would do normally. Then, the contents of the dictionary are displayed.
After that, I used the update method for adding a new key/value pair – have a look:
The code:
#Creating a dictionary a_directory = {'Name': 'Hina','Email': 'test@gmail.com', 'Phone':2817484758} print (a_directory) #Adding a new key/value pair a_directory.update( {'Address' : 'house 102, street 10'} ) print ("After using the update method:") print (a_directory)
The output:
{‘Name’: ‘Hina’, ‘Email’: ‘test@gmail.com’, ‘Phone’: 2817484758}
After using the update method:
{‘Name’: ‘Hina’, ‘Email’: ‘test@gmail.com’, ‘Address’: ‘house 102, street 10’, ‘Phone’: 2817484758}
Updating the value of an existing key example
The example below shows updating the “phone” key’s value by using the update method:
#Creating a dictionary a_directory = {'Name': 'Hina','Email': 'test@gmail.com', 'Phone':2817484758} print (a_directory) #Updating an existing item a_directory.update( {'Phone' : 3527894562} ) print ("After using the update method:") print (a_directory)
The result:
{‘Name’: ‘Hina’, ‘Phone’: 2817484758, ‘Email’: ‘test@gmail.com’}
After using the update method:
{‘Name’: ‘Hina’, ‘Phone’: 3527894562, ‘Email’: ‘test@gmail.com’}
You can see, the phone number is updated rather than appending a new key/value in the specified dictionary.