Add to Dictionary in Python

Add to Dictionary in Python

  1. Add to Dictionary in Python
  2. Add to Dictionary Using the Assignment Operator in Python
  3. Add to Dictionary Using the Update() Function in Python
  4. Add to Dictionary Without Overwriting in Python

We will introduce a method of how to add to a dictionary using Python.

Add to Dictionary in Python

Dictionary is one of the built-in data types in Python. Key-value pairs make up dictionary items.

In Python, there are several ways to add to a dictionary. Let’s look at some of the many methods for appending entries to a Python dictionary.

An assignment operator is used to create a dictionary in Python programming. You may use the assignment operator with the dictionary key in creating a new key for the dictionary.

Because dictionaries in Python are changeable, using the assignment operator merely adds new keys to the data structure.

The dictionary contains keys and values. The elements are added to the dictionary according to the keys assigned to them.

These elements are called values. In the dictionary, values are stored like this:

# python
dict[key] = value

Add to Dictionary Using the Assignment Operator in Python

The assignment operator automatically updates the values if a key exists in the dictionary. Let’s look at an example below to understand how this works in the dictionary.

If a key exists in the dictionary, the assignment operator will immediately replace the values.

# python
dictionary = {"1":"Dua","2":"Alex","3":"Husnain"}

print("Orignal dictionary is :" ,dictionary)

dictionary["2"]= "Babbar"

dictionary["4"]= "Azam"

print("updated dictionary is : " ,dictionary)

The output below shows the original and the updated dictionary.

add to dictionary in python

As seen from the above example, we can easily add items to the dictionary based on the keys. If the keys and values already exist, they will replace the value of the key.

Now, let’s discuss another method of update() that can be used to update the items in the dictionary.

Add to Dictionary Using the Update() Function in Python

The update() function is also used to update the dictionary; this function adds new keys and values to the dictionary or assigns new elements to the keys in the dictionary.

Let’s go through an example and update the data in the dictionary using the update() function as shown below.

# python
dictionary = {"1":"Dua","2":"Alex","3":"Husnain"}

print("Orignal dictionary is :" ,dictionary)

dictionary.update({"1":"Babar"})

dictionary.update({"4":"Azam"})

print("updated dictionary is : " ,dictionary)

Output:

add to dictionary in python using update function

As seen from the above code, we tried to update the values of keys 1 and 4. The value of key 1 gets updated while the new key and value are added for key 4.

If we want to add the key and values even if there are duplicates, we can do this without overwriting it and using the if statement.

Add to Dictionary Without Overwriting in Python

In the above example, some of the values are added to the keys by overwriting the original value in both functions we’ve looked at so far. Using the if statement in Python, the values are added to the keys without overwriting the original value of the key.

So let’s go through an example and check how we can skip overwriting and add the values in the dictionary as shown below.

# python
dictionary = {"1":"Dua","2":"Alex","3":"Husnain"}

print("Orignal dictionary is :" ,dictionary)

if '2' not in dictionary.keys():
    dictionary['2'] = "Babar"

elif '5' not in dictionary.keys():
    dictionary['5'] = "Azam"

print("updated dictionary is : " ,dictionary)

Output:

add to dictionary in python using if statement

As you can see from the above example, when our program tried to add the value for key 2, it didn’t get updated because there was already a value present for key 2. On the other hand, when we tried to add the value for key 5 that does not exist, it was added easily.

Rana Hasnain Khan avatar Rana Hasnain Khan avatar

Rana is a computer science graduate passionate about helping people to build and diagnose scalable web application problems and problems developers face across the full-stack.

LinkedIn

Related Article - Python Dictionary

  • Check if a Key Exists in a Dictionary in Python
  • Convert a Dictionary to a List in Python
  • Get All the Files of a Directory
  • Find Maximum Value in Python Dictionary
  • Delete a File and Directory in Python
  • Sort a Python Dictionary by Value