"
This article is part of in the series

This article discusses how you can use the update() function to modify the keys of dictionaries.

Python's dictionaries are key-value pairs where the values can be of any data type, but the keys must be of a single, immutable data type. A dictionary is declared with curly brackets, and the key-value pairs are declared with a colon (:) between them.  

The nice thing about these useful data structures is that you can update the keys and values without much hassle. There are a few different ways of doing it, but most developers rely on the update() function for this purpose.

This article discusses how you can use the update() function to modify the keys of dictionaries. As you'd expect, this guide requires you to have an understanding of how Python dictionaries work.  

Syntax of the update() Function

To use the update() function, you must use this syntax:

dict.update(iterable)

Here, "dict" is the dictionary you want to modify or add data to. The "iterable," on the other hand, is a placeholder for any Python iterable that holds key-value pairs. So, you can define a tuple or list inside the brackets where "iterable" is. 

How to Update a Python Dictionary with Another Dictionary

Consider that you're building a library of technology books to resell. You already have several copies of a couple of books in it. It's still a small library, but the good news is you've purchased a few dozen more copies of some popular books.

Writing this example in Python using dictionaries would look something like this:

# Current library of books and their quantities
current_library = {'Python Crash Course': 30, 'Data Science for Beginners': 25}

# New books you want to add
new_books = {'Advanced Python Programming': 15, 'Machine Learning Essentials': 20}

Now, to add the new books you've purchased to the current library, you can use the update() function. Following the syntax mentioned above, here's what this would look like:

# Updating the current library with new books
current_library.update(new_books)

# Now, let's see our updated library
print(current_library)

When the final line runs, you will see the following output:

{
    'Python Crash Course': 30,
    'Data Science for Beginners': 25,
    'Advanced Python Programming': 15,
    'Machine Learning Essentials': 20
}

As you may have noticed, the update() function puts the data in the dictionary you supply. It does not create a new dictionary and put the merged key-value pairs inside it.

Therefore, the update() method has a return type of None.

How to Update a Python Dictionary with Other Iterables

As mentioned earlier, it's possible to update Python dictionaries with elements from iterables that are not dictionaries. Let's say you purchase a few more books, but their data is stored in a tuple named "additional_books," like so:

additional_books = [('Python Cookbook', 33), ('Cracking the Coding Interview', 41)]

To seamlessly integrate these new book-price pairs into your existing dictionary, you can still use the trusty update() method:

current_library.update(additional_books)
print(current_library)

When the last line runs, you will see the following output:

{
   'Python Crash Course': 30, 
   'Data Science for Beginners': 25, 
   'Advanced Python Programming': 15, 
   'Machine Learning Essentials': 20, 
   'Python Cookbook': 33, 
   'Cracking the Coding Interview': 41
}

What Happens When a Dictionary is Updated with Repeated Keys?

The dictionaries in the examples above did not have any common keys. But what happens when one or more keys are repeated, and you use the update() function?

The answer is obvious, considering the behavior of the update() function so far. The value corresponding to the repeating key is overwritten. 

Let's say there is another list of tuples holding new data about the books after some sales. Let's call this list "updated_library."

updated_library = [('Advanced Python Programming', 10) ,  ('Machine Learning Essentials',15)]

You can now call the update() function on current_library. 

current_library.update(updated_library)
print(current_library)

You will notice that the values of two books in current_library have been overwritten by the values in updated_library, like so:

{
   'Python Crash Course': 30, 
   'Data Science for Beginners': 25, 
   'Advanced Python Programming': 10, 
   'Machine Learning Essentials': 15, 
   'Python Cookbook': 33, 
   'Cracking the Coding Interview': 41
}

Advantages of Using the update() Function to Modify Dictionaries

You could use other methods like dictionary comprehension and the ** operator to update dictionaries. 

In some instances, such as when you need to add values to a dictionary without modifying the original, techniques like dictionary comprehension can be more useful than the update() function.

That being said, using the update() function is the easiest and most straightforward way, whether you're a beginner or not. 

What's more, the update() function accepts iterables of all kinds, and not just dictionaries. Plus, you can also use keyword arguments with the function.

Potential Disadvantages of Using the update() Function to Modify Dictionaries

Though the update() function allows you to work with dictionaries without hassle, you must be careful to avoid some common pitfalls. Many developers make the mistake of attempting to modify a dictionary with a non-existent key.

If you do this, update() assumes that you need a new key-value pair added to the dictionary you have invoked it from. This can cause a lot of problems, especially if you're using numerical keys. 

Sometimes, developers mistakenly update a dictionary using a non-iterable object. This doesn't work and throws a TypeError, as the update() method expects an iterable.

Conclusion

The update() function in Python is a helpful tool for modifying dictionaries easily. Besides adding new key-value pairs, you can use it to merge two dictionaries in Python. It has the return type of None, meaning it updates the supplied Python dictionary. 

So, a potential disadvantage of using the function is that it cannot create a new dictionary. It can also cause problems if you're not careful, like making new pairs accidentally or trying to use it with things that aren't iterables. 

But on the plus side, it can quickly add new info from other dictionaries or even lists of pairs. So, overall, the update() function is super handy as long as you're careful using it. Use it wisely to make your Python code better!