Python Add to Dictionary: In Python, Dictionary is an unordered collection of Data( Key: Value Pairs ), which are separated by a comma( , ).

Dictionary is one of the most important Data Type in Python and the syntax to create a dictionary named “student” is as follow:

				
					student = {'Name': 'Alex', 'RollNo': 18, 'Section': 'B'}
				
			

As we have 3 elements in the student dictionary. So, if you want to print the value of these elements, you can do so by using the following command.

				
					print(student['Name'])
print(student['RollNo'])
print(student['Section'])
				
			

OUTPUT:
Alex
18
B

Now that we have seen the basics of creating a dictionary. Let’s see how you can perform Python add to dictionary or add elements in the existing directory.

However, let’s first take a look at how to update the elements of the existing dictionary.

Table of Contents

Updating Existing element in Dictionary

For updating the existing element of the dictionary, you just need the key of the particular element of the dictionary.

For example – If you want to update the ‘Name’ element from ‘Alex’ to ‘John’. Then the syntax to do so is as follow:

				
					student = {'Name': 'Alex', 'RollNo': 18, 'Section': 'B'}
print(student) #before updating

student['Name'] = 'John'
print(student) #after updating
				
			

OUTPUT:
{‘Name’: ‘Alex’, ‘RollNo’: 18, ‘Section’: ‘B’}
{‘Name’: ‘John’, ‘RollNo’: 18, ‘Section’: ‘B’}

Python Add to Dictionary by using subscript notation

Now that you know the process of updating the existing element. Let’s move on to our main topic which is how you can add new elements in our existing directory.

So, let’s take an example that we have already created a directory names ‘student’, which have 3 elements and we have to add more element in it.

				
					student = {'Name': 'Alex', 'RollNo': 18, 'Section': 'B'}
print(student) #before adding the elements

student['Marks'] = 430
student['Course'] = 'Engineering'
print(student) #after adding the elements
				
			

OUTPUT:
{‘Name’: ‘Alex’, ‘RollNo’: 18, ‘Section’: ‘B’}
{‘Name’: ‘Alex’, ‘RollNo’: 18, ‘Section’: ‘B’, ‘Marks’: 430, ‘Course’: ‘Engineering’}

So, this is how you can add the new element in the dictionary.

In addition, if you want to add the element in certain conditions only, then you can do so by using the If Condition.

For example – If I want to add the new element only if it is not already present in the dictionary. Then you can do so by using If Condition.

				
					student = {'Name': 'Alex', 'RollNo': 18, 'Section': 'B'}

if 'Age' not in student.keys():
     student['Age'] = 19

print(student)
				
			

OUTPUT:
{‘Name’: ‘Alex’, ‘RollNo’: 18, ‘Section’: ‘B’, ‘Age’: 19}

Python Add to Dictionary by using Update Method

Now that we have seen the Subscript Notation Method for adding the new elements in the dictionary.

Let’s take a look at another method for adding a new element in dictionary which is update() Method.

Update Method is more suitable, In case you want to add lot of new elements in your dictionary.

				
					student = {'Name': 'Alex', 'RollNo': 18, 'Section': 'B'}
print(student) #before using update() method

student.update({'course':'Engineering'}) #adding single element
print(student) #after adding single element using update

student.update({'Marks':430, 'Age':19})
print(student)
				
			

OUTPUT:
{‘Name’: ‘Alex’, ‘RollNo’: 18, ‘Section’: ‘B’}
{‘Name’: ‘Alex’, ‘RollNo’: 18, ‘Section’: ‘B’, ‘course’: ‘Engineering’}
{‘Name’: ‘Alex’, ‘RollNo’: 18, ‘Section’: ‘B’, ‘course’: ‘Engineering’, ‘Marks’: 430, ‘Age’: 19}

With update() method, Beside adding new elements, you can update the existing element of your dictionary as well.

				
					student = {'Name': 'Alex', 'RollNo': 18, 'Section': 'B'}
print(student) #before updating

student.update(Name :'John')
print(student) #after updating
				
			

OUTPUT:
{‘Name’: ‘Alex’, ‘RollNo’: 18, ‘Section’: ‘B’}
{‘Name’: ‘John’, ‘RollNo’: 18, ‘Section’: ‘B’}

Best Course

-> Best Course for learning Python: Python for Everybody Specialization

Adding a dictionary to another dictionary

Now that we know how to update and add new elements in the dictionary.

Let’s take a look at another most important topics which is Adding an old dictionary to another dictionary, which you can do by using update() method.

For example – if you have 2 dictionary named ‘student’ and ‘contact’ and you want to add the elements of ‘contact’ dictionary to the ‘student’ dictionary. Then you can do so as follow:

				
					student = {'Name': 'Alex', 'RollNo': 18, 'Section': 'B'}
contact = {'PhoneNo':1234567890, 'Country':'USA', 'PinCode':88901}
print(student) #before adding the contact dictionary

student.update(contact)
print(student) #after adding the contact dictionary
				
			

OUTPUT:
{‘Name’: ‘Alex’, ‘RollNo’: 18, ‘Section’: ‘B’}
{‘Name’: ‘Alex’, ‘RollNo’: 18, ‘Section’: ‘B’, ‘PhoneNo’: 1234567890, ‘Country’: ‘USA’, ‘PinCode’: 88901}

Hope you like the article on Adding to Directory in Python. We have also created an article on Python Projects for Beginners, which you must take a look.

Leave a Reply