In this tutorial, learn how to add new key-value pair in Dictionary using Python. The short answer is to use the Python update() function put new items.

You can add single elements as well as multiple items to Dictionary. Each new element comes with the new key with the associated new value. If you want to learn about Dictionary in Python, you may like to read Python Dictionary create, update and delete with Examples.

Add New Key Value Pair In Dictionary With Python

If you want to add a newly chosen key and value to the Dictionary. You have to use the new key and assign the new value to it. Check the below example to assign a new value with the new key.

The below example perform the addition of the new element to the end of the Dictionary items. By using this method you can also add new value to the existing element.

Output

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

The above example contains the newly added element at the end of the Dictionary. The new element key is ‘five’ and its associated value is 33.

However, the above example is not the only method you can use. You can also add the new element using the Python update function. Read further to see the method of using this function.

How to Add New Key Value Pair in Dictionary in Python

Append New Item in Dictionary With Python Update()

If you want to add a new key with its associated new value. You can use the update() function and pass the element as the argument. The passed argument should contain the key and value. Also, enclose the new item within the curly({}) brackets inside the function.

On execution of the code, it adds a new item to the end of the Dictionary.

Output

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

The above example contains the newly added element to the end. The newly appended element is with key ‘six’ and its associated value is ‘Billy’.

This is all about adding the single element to the Dictionary. However, you can also add multiple items together to the Dictionary using the same function. Read the below section to perform this task.

Multiple Key Value Pair Addition in Dictionary

In addition to above all, you can add multiple elements to the Dictionary using the update(). In the above example, you have passed the single element to add a single element.

To add multiple elements, you have to pass multiple items as the argument of the update function. The argument with multiple elements should be enclosed within the curly({}) brackets. See the example below to add multiple items to your Dictionary in Python.

Output

{‘one’: ‘Ram’, ‘two’: 13, ‘three’: ‘Jordge’, ‘four’: ‘Gill’, ‘seven’: ‘Kelly’, ‘eight’: ‘Brock’}

The above example contains the two newly added elements to the Dictionary. However, you can add more elements together using the update function. You just have to put more elements as the argument of the function.

Hope, you like this post of how to add new key-value pair in Dictionary using Python. If you have any query regarding the tutorial, please comment below.

Also tell me, which method you are using to add single and multiple elements to Dictionary in Python.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.