How To Update A Dictionary In Python?

A dictionary is a collection of key-value pairs. It stores the key as a value and vice versa. We can access the values in dictionaries using indexing, slicing, or direct lookup. In Python, dictionaries are a standard data structure. They are used to store key-value pairs. The keys are the unique identifiers for each item, whereas the values are the attributes of that item. However, You can create a dictionary by passing keyword arguments to the dict() function of Python. The keys and values can be any type of object in python. Here on this page, two methods are discussed with a step-by-step understanding of how they work. The last method finds you a tricky one but computationally easy to update a dictionary.

It is possible to create a dictionary from scratch and update or delete one from memory. Dictionaries can be updated with the update() command. 

Here is how:

* Working on anaconda environment Jupyter Notebook

Using the update() function to add new keys and values in a dictionary in Python

There are specific ways to update the dictionary and make changes in the dictionary in Python. In this method, we will shed light on the update() function to add new keys and values in the dictionary in Python. 

The execution process goes in the following manner:

– create a dictionary enclosed in curly brackets and define a key in “ ” commas and place “:” after the key name, and then assign the value for the respective key enclosed value in “ ” commas. That’s how the key is formatted and created. Another way to create a dictionary is using formkey(). 

– now call the update() function and pass the new key as an argument in the function you want to add to the dictionary to update the dictionary in Python. 

The next step is to print the updated dictionary using the simple print() command. 

cuisines = {
  "dish01": "Thai",
  "dish02": "Moroccan",
  "dish03": "Turkish"
}
cuisines.update({"dish04" : "Chinese" })
print(cuisines)
{'dish01': 'Thai', 'dish02': 'Moroccan', 'dish03': 'Turkish', 'dish04': 'Chinese'}

By defining a function with class and “ | ” operator.

By defining functions within the class, it’s possible to update a dictionary in Python. Here the leading dictionary is defined outside the class, so it becomes our global variable. Now this global variable, our ultimate dictionary, can be assessed anywhere within the class using its name, “menu”. 

The execution process updates the dictionary in the following sequence. Suppose you have a dictionary d containing these 3 values and want to append the dictionary with 2 more keys-value. In that case,n is possible by defining a function in a class, using the | operator within the specified function, and returning the updated dictionary.

  •  Now declare a global variable “cuisines” outside the class. 
  • Create a class with a unique name, “MENU” now, to give justice to the Python script, inject “:” at the end of the class name.
  • Now declare the static variables within the class, which is menu_update.
  • Now define the function by passing self as an argument. “self” allows us to access the class attributes. Otherwise, it will give the type error.
  • Then return the updated dictionary after computing the leading dictionary “cuisines”  with the updated dictionary “menu_update” with bitwise or |  operator. A Bitwise OR produces 1 if at least one input is 1. It is only possible to have a zero output if both inputs are 0.
  •  Finally, create an object “cuisines_update” for the class MENU(). Then call the key function and print the updated dictionary.
#simple dictionary with key-values
cuisines = {
  "dish01": "Thai",
  "dish02": "Moroccan",
  "dish03": "Turkish"
}
class MENU:
    menu_update={"dish04" : "Chinese"}
    def key(self):
        return  cuisines | MENU.menu_update
cuisines_update = MENU()
print("\nupdate dictionary :", cuisines_update.key())
update dictionary : {'dish01': 'Thai', 'dish02': 'Moroccan', 'dish03': 'Turkish', 'dish04': 'Chinese'}

Conclusion

In this tutorial, two methods are discussed to update the dictionary in Python. One approach uses the built-in function .update() and the second approach to asses it by defining a function within the class. However, after reading it, you now understand how to create a dictionary and add new keys and values in Python. 

If you want to learn more about Python Programming, Visit Python Programming Tutorials.

Leave a Comment

Your email address will not be published. Required fields are marked *