Open In App

Update a Dictionary in Python using For Loop

Last Updated : 16 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Updating dictionaries in Python is a common task in programming, and it can be accomplished using various approaches. In this article, we will explore different methods to update a dictionary using a for loop.

Update a Dictionary in Python Using For Loop in Python

Below are some of the approaches by which we can update a dictionary in Python by using a for loop:

  1. Using a Simple For Loop
  2. Using Dictionary Comprehension
  3. Using the items() Method
  4. Using zip() and keys()

Update a Dictionary Using a Simple For Loop

In this approach, we uses a for loop to iterate over the keys in the ‘update_dict’. For each key, we check if it already exists in the ‘target_dict’. If it does, we update its value; otherwise, we add a new key-value pair to the ‘target_dict’. This approach ensures that all key-value pairs from ‘update_dict’ are incorporated into ‘target_dict’.

Python3




# Sample dictionaries
target_dict = {'a': 1, 'b': 2, 'c': 3}
update_dict = {'b': 5, 'd': 7}
 
# Updating using a for loop
for key in update_dict:
    if key in target_dict:
        target_dict[key] = update_dict[key]
    else:
        target_dict[key] = update_dict[key]
 
print(target_dict)


Output

{'a': 1, 'b': 5, 'c': 3, 'd': 7}


Update a Dictionary Using Dictionary Comprehension

This approach utilizes dictionary comprehension to create a new dictionary (‘updated_dict’). For each key in ‘target_dict’, it checks if the key exists in ‘update_dict’. If it does, the corresponding value from ‘update_dict’ is used; otherwise, the value from ‘target_dict’ is retained. This results in a dictionary containing all keys from ‘target_dict’ with updated values from ‘update_dict’.

Python3




# Sample dictionaries
target_dict = {'a': 1, 'b': 2, 'c': 3, 'd':6}
update_dict = {'b': 5, 'd': 7}
 
# Updating using dictionary comprehension
updated_dict = {
    key: update_dict[key] if key in update_dict else target_dict[key] for key in target_dict}
print(updated_dict)


Output

{'a': 1, 'b': 5, 'c': 3, 'd': 7}


Update a Dictionary Using the items() Method

In this approach, we use the ‘items()’ method to iterate over key-value pairs in ‘update_dict’. For each pair, we update the corresponding key in ‘target_dict’ with the new value. This method ensures that all key-value pairs from ‘update_dict’ are incorporated into ‘target_dict’.

Python3




# Sample dictionaries
target_dict = {'a': 1, 'b': 2, 'c': 3}
update_dict = {'b': 5, 'd': 7}
 
# Updating using the items() method
for key, value in update_dict.items():
    target_dict[key] = value
 
print(target_dict)


Output

{'a': 1, 'b': 5, 'c': 3, 'd': 7}


Update a Dictionary Using zip() and keys()

In this approach, we use the zip() function to combine keys and values from ‘update_dict’. The for loop iterates over these pairs, updating the corresponding keys in ‘target_dict’ with the new values. This approach is concise and elegant for updating dictionaries with corresponding key-value pairs.

Python3




# Sample dictionaries
target_dict = {'a': 1, 'b': 2, 'c': 3}
update_dict = {'b': 5, 'd': 7}
 
# Updating using zip() and keys()
for key, value in zip(update_dict.keys(), update_dict.values()):
    target_dict[key] = value
 
print(target_dict)


Output

{'a': 1, 'b': 5, 'c': 3, 'd': 7}


Conclusion

Updating dictionaries in Python is a versatile operation, Whether opting for a simple for loop, leveraging built-in methods like update(), utilizing dictionary comprehension, or exploring iterations with items() and zip(), each method offers a reliable way to update dictionary contents.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads