Answer: To add new keys to a dictionary in Python you can use the update() method.
Dictionary in Python is a built-in data type that stores and manages data in pairs of keys and values. Unlike lists or tuples, which are ordered, dictionaries are unordered collections, they do not have their items stored in any given sequence.
Table of Contents:
Methods to Add New Keys to a Dictionary in Python
Python provides various methods to perform operations on dictionaries, following are some methods that are used to add new keys to a dictionary in Python:
Method 1: Using the ‘=’ Assignment Operator to Add New Keys to a Dictionary in Python
This method is the simplest way to add a new key to a dictionary. We can insert a new key into the dictionary directly by giving a value to a new key.
Example:
Output:
Note: In case the key is already available in the dictionary, it updates the value related to that key. Otherwise, if it does not exist, then a new key-value pair will be created.
Method 2: Using the update() Method to Add New Keys to a Dictionary in Python
The update() method in Python is used to insert new key-value pairs in the dictionary with a single operation. With the use of the update() method, you can also merge two or more dictionaries.
Example:
Output :
Note: It updates the value if the key already exists in the dictionary; otherwise, it adds a new key-value pair.
Method 3: Using the setdefault() Method to Add New Keys to a Dictionary in Python
This method is commonly used in data structures like dictionaries. It checks if a specific key exists within the collection. If the key is present, the method simply returns its value without any modifications, if the key is absent, it assigns a default value to that key and returns it.
Example 1: Using the setdefault() method if the key is present
Output:
Example 2: Using setdefault() method if the key is not present
Output:
Explanation:
The setdefault() method offers a clean and efficient way to handle cases where you need to ensure a key exists in the dictionary and potentially assign a default value to it.
Method 4: Using fromkeys() Method to Add New Keys to a Dictionary in Python
The method fromkeys() in the dictionary is used to create a new dictionary from a sequence of keys having a specified value
Syntax:
dict.fromkeys(keys, value = None)
Here, Keys refer to the sequence or collection (list or tuple) of keys that you want to include in the dictionary, and Value is assigned to the key and if it is not assigned, by default it is None.
Example 1:
Output:
Explanation:
In the above example, the fromkeys() method creates a dictionary with keys (‘A’, ‘B’, ‘C’) and have the same default value ‘0’
Example 2: Default Value None
Output:
Explanation:
If no value is assigned then the keys are assigned value as None by default.
Method 5: Using dict() Method to Add New Keys to a Dictionary in Python
The dict() is used to create a dictionary from a list of key-value pairs and this method creates and combines with existing dictionaries.
Example:
Output:
Explanation:
The dict() method is similar to the update() method for merging key-value pairs into the dictionaries but they work a bit differently.
Conclusion
Adding new keys to a dictionary can be done in several ways, such as using the ‘=’ assignment operator, to add or update keys directly, ‘update()’ to merge multiple key-value pairs, ‘setdefault()’ to ensure keys exist with default values, ‘fromkeys()’ to build dictionaries from sequences, and ‘dict()’ to construct new dictionaries.