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 Python add to dictionary.

What is Python dictionary?

Python add to dictionary

Before learning how to add an item to dictionary, 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. 

Python add to dictionary

Now, let’s turn in Python add to dictionary. 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 Python add to dictionary. 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’}

Using update () method

If you need to upload or add a lot of value to the dictionary, this Python add to dictionary is ideal for you

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 Python add to dictionary. However, working with Python is not easy, especially when you’re a beginner. Therefore, to avoid unexpected problems and issues, we highly recommend you contact professional developers. In this case, you should contact ArrowHitech – one of the best outsourcing IT in Vietnam. Having more than 12 years of experience, we can help you handle difficulties in not only coding, but also in eCommerce. What are you waiting for? Contact us here or via email: support@arrowhitech.com or hotline + 84 243 7955 813

Tags

Share