Table of Contents
ToggleAdding dictionary items in Python refers to inserting new key-value pairs into an existing dictionary. Dictionaries are mutable data structures that store collections of key-value pairs, where each key is associated with a corresponding value.
Adding items to a dictionary allows you to dynamically update and expand its contents as needed during program execution.
We can add dictionary items in Python using various ways such as −
The square brackets [] in Python is used to access elements in sequences like lists and strings through indexing and slicing operations. Additionally, when working with dictionaries, square brackets are used to specify keys for accessing or modifying associated values.
You can add items to a dictionary by specifying the key within square brackets and assigning a value to it. If the key is already present in the dictionary object, its value will be updated to val. If the key is not present in the dictionary, a new key-value pair will be added.
marks = {"Savita":67, "Imtiaz":88, "Laxman":91, "David":49}
print ("Initial dictionary: ", marks)
marks['Kavya'] = 58
print ("Dictionary after new addition: ", marks)
It will produce the following output −
Initial dictionary: {'Savita': 67, 'Imtiaz': 88, 'Laxman': 91, 'David': 49}
Dictionary after new addition: {'Savita': 67, 'Imtiaz': 88, 'Laxman': 91, 'David': 49, 'Kavya': 58}
The update() method in Python dictionaries is used to merge the contents of another dictionary or an iterable of key-value pairs into the current dictionary. It adds or updates key-value pairs, ensuring that existing keys are updated with new values and new keys are added to the dictionary.
marks = {"Savita":67, "Imtiaz":88}
print ("Initial dictionary: ", marks)
marks.update({'Kavya': 58, 'Mohan': 98})
print ("Dictionary after new addition: ", marks)
Output:
Initial dictionary: {'Savita': 67, 'Imtiaz': 88}
Dictionary after new addition: {'Savita': 67, 'Imtiaz': 88, 'Kavya': 58, 'Mohan': 98}
Unpacking in Python refers to extracting individual elements from a collection, such as a list, tuple, or dictionary, and assigning them to variables in a single statement.
marks = {"Savita":67, "Imtiaz":88, "Laxman":91, "David":49}
print ("marks dictionary before update: \n", marks)
marks1 = {"Sharad": 51, "Mushtaq": 61, "Laxman": 89}
newmarks = {**marks, **marks1}
print ("marks dictionary after update: \n", newmarks)
The union operator in Python, represented by the | symbol, is used to combine the elements of two sets into a new set that contains all the unique elements from both sets. It can also be used with dictionaries in Python 3.9 and later to merge the contents of two dictionaries.
marks = {"Savita":67, "Imtiaz":88, "Laxman":91, "David":49}
print ("marks dictionary before update: \n", marks)
marks1 = {"Sharad": 51, "Mushtaq": 61, "Laxman": 89}
newmarks = marks | marks1
print ("marks dictionary after update: \n", newmarks)
The collections.defaultdict() method in Python is a subclass of the built-in “dict” class that creates dictionaries with default values for keys that have not been set yet. It is part of the collections module in Python’s standard library.
from collections import defaultdict
# Using int as the default factory to initialize missing keys with 0
d = defaultdict(int)
d["a"] += 1
print(d)
# Using list as the default factory to initialize missing keys with an empty list
d = defaultdict(list)
d["b"].append(1)
print(d)
# Using a custom function as the default factory
def default_value():
return "N/A"
d = defaultdict(default_value)
print(d["c"])
Output:
defaultdict(, {'a': 1})
defaultdict(, {'b': [1]})
N/A
