Python add to dictionary: How to add an item to a dictionary

In Python, a dictionary is an unordered collection using key – value structure. Thus, it stores value like a map. If you’re working with Python, high chance that you might want to add one, or some items to a dictionary. Then, in this guideline, we will provide you with comprehensive tutorials about how to add items to a dictionary in python

What is Python dictionary?

Python add to dictionary

Before learning how to add items to a dictionary in python, we need to understand Python thoroughly. As mentioned above, Python dictionary use key – value structure to store value. Thanks to this, you will be able to map keys to values. Therefore, when you want to find any value, just simply reference the name for the value you want to retrieve

Each key – value pair in a Dictionary is separated by a colon (“:”) and a comma (“,”) will separate each key. In Particular, keys in the Python Dictionary should be unique. Moreover, they have to be of immutable data type, for example, String, Integers and Tuples. 

How to add items to a dictionary in python

Now, let’s turn in how to add items to a dictionary in python. But first, to do this, you need to create a Python dictionary.

class my_dictionary(dict): 

def __init__(self): 

self = dict() 

def add(self, key, value): 

self[key] = value 

dict_obj = my_dictionary() 

dict_obj.add(1, ‘Geeks’) 

dict_obj.add(2, ‘forGeeks’) 

print(dict_obj) 

Output:

{1: ‘Geeks’, 2: ‘forGeeks’}

After this, it’s time for add items to a dictionary in python We will provide you with 4 common methods to successfully carry this out

Using Subscript notation

With this method, you can create a new key – value pair in the dictionary by assigning value to that key. In case the key exists, the value which it is pointing to will be overwritten. On the other hand, if the key doesn’t exist, it will be added and point to that value

Here is the code of this method

dict = {‘key1′:’geeks’, ‘key2′:’fill_me’} 

print(“Current Dict is: “, dict) 

Then, to add value to your Python dictionary, you can use this syntax: Dictionary_Name[New_Key_Name] = New_Key_Value 

Below is the example of this step

dict[‘key2’] = ‘for’

dict[‘key3’] = ‘geeks’

print(“Updated Dict is: “, dict) 

Output

Current Dict is:  {‘key1’: ‘geeks’, ‘key2’: ‘fill_me’}

Updated Dict is:  {‘key3’: ‘geeks’, ‘key1’: ‘geeks’, ‘key2’: ‘for’}

Add items to a dictionary in python: Using update () method

To understand this method, let’s consider this example

dict = {‘key1′:’geeks’, ‘key2′:’for’} 

print(“Current Dict is: “, dict) 

Now, you add your ‘key3’ with your new value

dict.update({‘key3′:’geeks’}) 

print(“Updated Dict is: “, dict) 

Continue adding ‘key 4’ and ‘ key 5’ along with new values

dict1 = {‘key4′:’is’, ‘key5′:’fabulous’} 

dict.update(dict1) 

print(dict) 

And then, by assigning

dict.update(newkey1 =’portal’) 

print(dict) 

Using __setitem__ method 

Despite its simplicity, we don’t recommend using this Python add to dictionary method due to its poor performance. Here is the example code for this method

dict = {‘key1′:’geeks’, ‘key2′:’for’} 

dict.__setitem__(‘newkey2’, ‘GEEK’) 

print(dict) 

Output

{‘key2’: ‘for’, ‘newkey2’: ‘GEEK’, ‘key1’: ‘geeks’}

Using operator

This method will help you merge old dictionary with the new value in a different dictionary

dict = {‘a’: 1, ‘b’: 2} 

Then, create new dictionary

new_dict = {**dict, **{‘c’: 3}} 

print(dict) 

print(new_dict) 

Output

{‘b’: 2, ‘a’: 1}

{‘b’: 2, ‘c’: 3, ‘a’: 1}

In conclusion

We hope to deliver you some basic knowledge about how to add items to a dictionary in python. If you want to learn more about Python, as well as getting the best of Python, don’t hesitate to contact ArrowHitech – a leading IT outsourcing company in Vietnam to get excellent mobile app development services .Contact us here now!

Tags

Share