Python - Add Dictionary Items
In Python, dictionaries are a built-in data structure that stores key-value pairs. Adding items to a dictionary is a common operation when you're working with dynamic data or building complex data structures. This article covers various methods for adding items to a dictionary in Python.
Adding Items Using Direct Assignment
The most straightforward way to add a new item to a dictionary is by directly assigning a value to a key. If the key already exists, this will update the value associated with that key. If the key does not exist, it will add a new key-value pair.
d = {1: 10, 2: 20, 3: 30}
d[4] = 40 # Add a new key-value pair
print(d)
In this example, we added a new key-value pair with key 4 and value 40. If the key 4 already existed, it would update the value.
Let's take a look at other methods of adding items to a dictionary:
Table of Content
Adding Multiple Items Using update() Method
If you want to add multiple items at once or update existing items, update() method is the most efficient way. This method accepts another dictionary or an iterable of key-value pairs and adds those pairs to the original dictionary. If any of the keys already exist, their values are updated.
d = {1: 10, 2: 20, 3: 30}
d.update({4: 40, 5: 50}) # Add multiple key-value pairs
print(d)
In this example, we added two new key-value pairs: 4: 40 and 5: 50. The update() method can be used to add multiple items in one operation.
If you have an iterable (such as a list or tuple) of key-value pairs, you can pass it to the update() method to add those pairs to the dictionary.
d = {1: 10, 2: 20, 3: 30}
a = [(4, 40), (5, 50)]
d.update(a) # Add items from an iterable
print(d)
Here, the list a contains tuples, where each tuple represents a key-value pair. These pairs are added to the original dictionary using update().
Adding Items Using setdefault() Method
The setdefault() method is another way to add an item to a dictionary. This method checks if a key already exists:
- If the key exists, it returns its value.
- If the key does not exist, it adds the key with a specified default value.
d = {1: 10, 2: 20, 3: 30}
d.setdefault(4, 40) # Adds key 4 with value 40
print(d)
In this example, setdefault() adds key 4 with value 40. If key 4 already existed, its value would remain unchanged.
Adding Items Using Dictionary Comprehension
For more complex scenarios where you want to add or transform items based on conditions, dictionary comprehension is a powerful tool. This allows you to dynamically create new dictionary entries.
d1 = {1: 10, 2: 20, 3: 30}
# Add items where the key is greater than 2
d2 = {k: v * 2 for k, v in data.items() if k > 2}
print(d2)
In this example, we used dictionary comprehension to create a new dictionary where keys greater than 2 have their values doubled.
Adding Items Using a Loop
In cases where you need more control over how items are added, you can use a loop to add items to a dictionary.
d = {1: 10, 2: 20, 3: 30}
for i in range(4, 6):
d[i] = i * 10 # Add new items in a loop
print(d)
Here, a loop is used to add new items to the dictionary by calculating values dynamically.