Adding a key-value pair to dictionary in Python
Python dictionaries are collections of unsorted key-value pairs. This article will examine a method for adding new key-value teams to an existing dictionary.
Dictionary in Python
With the aid of curly brackets and commas, Python enables you to build dictionaries from lists of entries. Dictionary key-value pairs are saved in pairs, with the critical element being the other. Values in dictionaries can be any data and duplicated, unlike keys, which cannot be repeated and need to be immutable.
The structure is wrapped in curly brackets, commas separate the elements, and colons separate each key and value (:). A dictionary without any words can be written as follows:
In Python, a dictionary is a group of objects that stores data as key-value pairs. The dictionary items can be accessed and changed based on their key. Dictionaries can be changed and have new words added to them.
By giving a value to a new key that makes reference to the dictionary's index, a single item can be added to a dictionary the shortest possible method. As an illustration, let's add a fresh key-value combination like this:
Cars['Audi'] = 1
Multiple entries can be added to dictionaries using Python.
Add a Key to a Python dictionary
A key-value pair can be added in several ways to an existing dictionary.
Include Key and Value
A dictionary's additional keys can be added by giving them a value. The value the key points to is overwritten if it already exists. The key must be presented using the dictionary's subscript notation as follows:
my_dictionary[new_key] = new_value
The dictionary will add this key-value pair. If Python 3.6 or later is used, it will be included as the dictionary's final entry.
Let's create a dictionary and then use the following method to add a new key-value pair:
Example
squares = {2: 2,8: 1, 6: 4}
squares[6] = 16 # Adding new key-value pair
print(squares)
Output

Dictionary Without Value-Add Key
With any techniques described, the user can substitute None for the value if you only want to add a key.
Example
squares = {2: 2,8: 1, 6: 4}
squares['x'] = None # Adding new key-value pair
print(squares)
Output

Add several Key-Value Pairs together with an update ()
Multiple key-value pairs can be added to an existing dictionary in Python. The update () method is used to accomplish this. This method adds new key-value teams to the dictionary using an argument of type dict or any iterable having a length of two, such as ((key1, value1),).
The new value replaces the key if it already exists in the dictionary.
Dictionary.update(new key=new value) is one way to give the keys and their matching values to this method as keyword arguments.
Adding additional keys and values to a dictionary is likely the most common practice.
Let's add many key-value pairs to a dictionary using the update () method:
Example
rainbow = {'violet' : 1}
# Update by passing the dictionary
new_key_values_dict = {'red': 2, 'green': 3}
rainbow.update(new_key_values_dict)
print("update by passing dictionary")
print(rainbow)
# Update by passing iterables
new_key_values_itr = (('yellow, 4'), ('indigo', 5))
rainbow.update(new_key_values_itr)
print("update by passing iterables")
print(rainbow)
# updating through iterable passing
rainbow.update(orange=6, Blue=7)
print("update using keyword arguments")
print(rainbow)
Output

Merge Operator (Python 3.9+) usage
The built-in dict class now supports the merge (|) and update (|=) operators of Python version 3.9.
These are efficient techniques for adding several key-value pairs to a dictionary. The keys and values of both specified dictionaries are combined into a new dictionary via the merge (|) operator. Then, we may add this finding to a new dictionary.
The second dictionary's key-value pairs are added to the first dictionary via the update (|=) operator, though. As a result, some key-value teams from another dictionary are added to the existing vocabulary.
Conclusion
Using subscript notation, we first created the key-value pair by giving each key in the dictionary a value. The update () method for adding multiple key-value teams to a dictionary was then examined. The Update () method has also been used with the types dictionary, tuple, and keyword arguments. Finally, we investigated the Merge and Update operators introduced in Python 3.9 and beyond.
The most common mechanism for adding new keys to an existing dictionary is the dictionary's update() method.