Python dictionary Append value

Python Dictionary

In this post, we are going to explore how to Python dictionary Append value is done using different methods, it can be a key-value pair or multiple key pairs.

There are two ways to append value to a python dictionary.

  • Using subscript notation: append value to Python dictionary
  • Update() Method :append value to Python dictionary

1. Using subscript notation


The subscript notation([]) use to add a new key-value pair or update an existing key’s value. we pass the key inside this operator [] and assign value to it using = (equal operator).

1.1 Appending new key-value pair

In this, we have an empty dictionary and we are adding a new key-value pair using the subscript notation([]).In this ‘math’ is key and the value is 100

dict_sub = {}
dict_sub['math'] = 100
print('after adding value to dictionary =',dict_sub)

Now we have new key ‘math‘ with value 100 in dictionary

Output

after adding value to dictionary = {'math': 100}

1.2.Update existing key’s value

If we use the subscript notation([]) to add a new key that already exists, then the value of the key will be updated with the new value.

In this code example, we are adding the key ‘Eng’ that is already existing in the dictionary has a value of 100. So, after assign the new value, the key ‘Eng‘ value will be overwritten by 90.

dict_sub = {'math':100,'Eng':100,'Chem':98}
#adding key-value pair in dictionary using []
dict_sub['Eng'] = 90
print('after adding value to dictionary =',dict_sub)

Output

after adding value to dictionary = {'math': 100, 'Eng': 90, 'Chem': 98}

1. 3.Add key does not exist in dictionary

Sometimes, we want to keep the value of the existing key except overriding it with the new value. So to achieve that first, we will check if the key does not exist then we will add new key-value pair else we will not. We are using subscript notation([]) to adding a key if not exist in the dictionary.

dict_sub = {'math':100,'Eng':100,'Chem':98}
key = 'sci'
if key not in dict_sub:
 dict_sub['sci'] = 100
else:
   print('key already exist in Dictionary')

print('after adding value to dictionary =\n',dict_sub)

Output

after adding value to dictionary =
 {'math': 100, 'Eng': 100, 'Chem': 98, 'sci': 100}

2. Python dict Append Using Update() Method


The update() is an inbuilt python dictionary method that takes a sequence as an argument, The sequence can pair of a key-value pair, a dictionary, iterable(tuple, list of tuples). It allows us to add a new key-value pair if does not exist in the dictionary, else update the value of the existing key.

Syntax

dict.update(iterable)

Parameter

Iterable: It can be a pair of key-value, dictionary or iterable of key-value pair (tuple, list of tuples) as an argument

Return Value : none

2.1.Add a key-value pair to a dictionary

In the below code we are adding a key-value pair {‘Phy’:98} using the update() method.Update() method returns none and updates the existing dictionary.

dict_sub = {'math':100,'Eng':100,'Chem':98}
#adding key-value pair using update() method
dict_sub.update({'Phy':98})
print('after adding value to dictionary =\n',dict_sub)

Output

after adding value to dictionary =
 {'math': 100, 'Eng': 100, 'Chem': 98, 'Phy': 98}

Adding a string key

If we are aware the key is of string type then we can directly add to dictionary,here we are adding string keyvalue {sci=100} pair in dictionary.

dict_sub.update(Sci=100)

This is dictionary after adding both key value pair {‘Phy’:98},{sci=100}.

Output

after adding value to the dictionary =
 {'math': 100,'Chem': 98, 'Phy': 98, 'Sci': 100}

2.2. Update an existing key’s value

If we are going to use the update() method on the key that already exists in the dictionary that it will update the value of that key. Here in this code example, we are adding the new key-value pair {‘chem’:100}. But the ‘chem’ key already exists. So the value of key ‘chem’ will be updated to 100.

dict_sub = {'math':100,'Eng':100,'Chem':98}
#updating existing key using update() method
dict_sub.update({'chem':100})
print('after adding value to dictionary =\n',dict_sub)

Output

after adding value to the dictionary =
 {'math': 100, 'Eng': 100, 'Chem': 98, 'chem': 100}

2.3.Add key if does not exist in Dictionary

Sometimes we have to keep the value of the existing key except overriding with the new value. So to achieve this first we will check if the key does not exist then we will add the key-value pair else we will not. Both subscript notation([]) and the update() method can be used to add a key if not exist in the dictionary.

dict_sub = {'math':100,'Eng':100,'Chem':98}
key = 'sci'
if key not in dict_sub:
 dict_sub.update({'sci':100 })
else:
   print('key already exist in Dictionary')

print('after adding value to dictionary =\n',dict_sub)

Output

after adding value to dictionary =
 {'math': 100, 'Eng': 100, 'Chem': 98, 'sci': 100}

Conclusion:

We have explored add/append value pairs in a Python dictionary. Now by using these methods, we can easily add/append a key to a dictionary.