How to add values to the dictionary in Python?


Python is a versatile and powerful programming language that has various built-in datatypes and one of the most useful built-in datatypes in Python is the dictionary which allows us to store and retrieve data in a key-value format, and we can easily add or remove values to a dictionary as compared to other built-in datatypes in Python.

In this article, we will be discussing how to add values to a dictionary in Python and what are the various methods to do the same.

What is a dictionary in Python?

Before we get started with adding values to dictionaries in Python, let us first understand what are the dictionaries and how they exactly work.

In Python, we have a built-in dataset which is called a dictionary is a mutable data type that mainly represents a collection of key-value pairs. Each key in a dictionary must be unique, and each key is associated with a particular value. In Python, we can create a dictionary using curly braces {} or the dict() constructor function. Below is the programming example where we have created a dictionary by using both the methods- curly braces {} and the dict () constructor function.

Example

# Creating a dictionary using {}
dictionary = {'Machine learning': 10, 'Artificial intelligence': 20, 'data mining': 30}
print(dictionary)
# Creating a dictionary using dict()
dict_new = dict(Machine_learning=10, Artificial_intelligence=20, data_mining=30)
print(dict_new)

Output

{'Machine learning': 10, 'Artificial intelligence': 20, 'data mining': 30}
{'Machine_learning': 10, 'Artificial_intelligence': 20, 'data_mining': 30}

How to add values to the dictionary in Python?

The process of adding values to a dictionary in Python is a very simple process. We can simply do this by assigning a value to a key in the dictionary. If the key is already there in the dictionary, then the value associated with that key will be updated with the new value that we have provided. If the key does not already exists then a new key-value pair value will be added to the dictionary.

We will be learning how to add a single key-value pair to a dictionary and multiple key-value pairs to a dictionary.

Adding a Single value to a dictionary

For adding a single value to a dictionary, we need to specify a new key-value pair. Below is an example to do that −

Example

# Creating a dictionary using {}
dictionary = {'Machine learning': 10, 'Artificial intelligence': 20, 'data mining': 30}
print(dictionary)
dictionary['Cloud computing']=40
print(dictionary)

Output

{'Machine learning': 10, 'Artificial intelligence': 20, 'data mining': 30}
{'Machine learning': 10, 'Artificial intelligence': 20, 'data mining': 30, 'Cloud computing': 40}

In the above program, we assigned the value 4 to the key “Basic”, creating a new key-value pair in the dictionary.

Adding multiple values to a dictionary in Python

If we want to add multiple key-value, we have to use the update() method. The update() function takes another new dictionary as an argument and then adds its key-value pairs to the existing dictionary.

Here's an example of how to add multiple new programming languages to the existing programming languages dictionary −

Example

# Creating a dictionary using {}
dictionary = {'Machine learning': 10, 'Artificial intelligence': 20, 'data mining': 30}
print(dictionary)
new_dict = {"Cloud computing": 20, "big data": 60, "tensorflow": 40}
dictionary.update(new_dict)
print(dictionary)

Output

{'Machine learning': 10, 'Artificial intelligence': 20, 'data mining': 30}
{'Machine learning': 10, 'Artificial intelligence': 20, 'data mining': 30, 'Cloud computing': 20, 'big data': 60, 'tensorflow': 40}

Updating values in a dictionary

If we want to update the value of the existing key of a particular dictionary we just need to assign a new value to that key. Below is the program example that shows how to update values in the dictionary −

Example

# Creating a dictionary using {}
dictionary = {'Machine learning': 10, 'Artificial intelligence': 20, 'data mining': 30}
print(dictionary)
dictionary['Machine learning']=40
print(dictionary)

Output

{'Machine learning': 10, 'Artificial intelligence': 20, 'data mining': 30}
{'Machine learning': 40, 'Artificial intelligence': 20, 'data mining': 30}

Conclusion

In this article, we discussed how to add values to a dictionary in Python. We got to know that adding values to a dictionary is a very simple and easy process, and we can do it by assigning a new value to an existing or a new key in the dictionary in Python or we can also use the update() method to add multiple key-value pairs to the existing dictionary. By using dictionaries, we can easily store or retrieve data in a key-value format in our Python program.

Updated on: 31-May-2023

250 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements