How to Add Element To Key In Dictionary Using Python

In this tutorial, learn how to add an element to key in Dictionary with Python. The short answer is to use the Python update() to append the new element to the existing key or add multiple key pairs. Update the existing key value for the accuracy of Dictionary elements.

The dictionary is the collection of unordered elements in Python. There are keys and the elements associated with these keys. If you want to know more about the Dictionary, you may like to read Python Dictionary.

Append New Value To Existing Key Of Dictionary in Python

There are keys pairs with associated values in the Dictionary as given below. If you want to change the value of the existing key in the given Dictionary. You have to assign the new value to the existing key using the below example.

The assigned new value to the existing key replaces the old value from the new one. You can also use this method to update the values or elements of the old keys.

Output

{‘one’: ‘Ram’, ‘two’: 13, ‘three’: ‘Jordge’, ‘four’: ‘Dev’}

The above example showing the new value assigned to the element with key ‘four’. The output contains the updated items of the dictionary after the replacement. If you want to update more key values of the Dictionary, you have to use the above example method many times.

Bonus: download a Free Python cheat sheet that will show you 20+ most important examples to learn in Python.

Add New Element With New Key in Dictionary Using Python

You can also add new items to the Dictionary in Python. To perform adding new items, you have to use the update() function of Python. The function takes Dictionary item as the argument to pass which you want to append.

The element appends to the end of the Dictionary using Python. You can append only single element or items in Dictionary using this method.

Note: One more thing here you should note here if you pass an argument with the matching key in Dictionary. The below example replaces the only the value of the existing key if it is different.

Output

{‘one’: ‘Ram’, ‘two’: 13, ‘three’: ‘Jordge’, ‘four’: ‘Gill’, ‘five’: ‘Kelly’}

The above example adds the new element to the end of the Dictionary in Python. At the start of the example, there are 4 item exists in the Dictionary. However, after the new addition of element, the Dictionary contains 5 items in the count.

The above all example works for the time when you want to add or update single element. If you want to add more elements to the Dictionary, you have to read further.

Update Dictionary With Multiple Key Pairs in Python

In addition to the above all methods, you can also add multiple key pairs to the Dictionary in Python. To add multiple items in the Dictionary, you again have to use the same update() function as you use in the above example.

The only difference from the above example is that you have to pass multiple key pairs as the argument of update(). The multiple key pairs are a comma(,) separated as given in the example below.

Output

{‘one’: ‘Ram’, ‘two’: 13, ‘three’: ‘Jordge’, ‘four’: ‘Gill’, ‘five’: ‘Kim’, ‘six’: ‘Billy’, ‘seven’: ‘Kelly’}

In the above example, the output contains the Dictionary with multiple additions of elements. At the start of the example, there are only 4 elements in the Dictionary. However, after the addition of multiple elements, the Dictionary contains 7 elements.

You can add as many items as you want in the Dictionary to append. If you pass the existing key item in the update()code replaces the associated old value of the key with the new value.

DOWNLOAD Free PYTHON CHEAT SHEET

You may also like to read

I hope you like this post on how to add or append element to key in Dictionary using Python.