Javatpoint Logo
Javatpoint Logo

Append (key: value) Pair to Dictionary

Dictionary is one of the most used data types in Python. It is an unordered collection of key: value pairs. Every value has a corresponding key that identifies it. A dictionary is a mutable collection, meaning we can modify the values. One factor that makes a dictionary unique among other data types is that it stores the mapping of key: value pairs while other data types store a single value as an element.

Example:

Code

Output:

{101: 'Ramya', 102: 'Sanya', 103: 'Sree'}
Ramya
Sanya
Sree

Given that a dictionary is mutable, we should be able to alter the values of existing keys and add new key: value pairs to the dictionary. This tutorial discusses how we can add a new key: value pair, into a dictionary.

1. Traditional way of using the key Subscripts:

We can assign the existing keys with the values we want. Python forgets the old value and updates the new value to the key. Using the same way, we can assign values to new keys, thus appending new pairs.

The subscript notation syntax is as follows:

Syntax:

Example:

Code

Output:

{101: 'Ramya', 102: 'Sanya', 103: 'Sree'}
{101: 'Ramya', 102: 'Priyanka', 103 : 'Ujjwala', 104: 'Sanya', 105: 'Sree'}

2. update() Method:

It is an inbuilt dictionary method designed to modify a dictionary. The method takes the {key: value} pair as an argument and adds it to the dictionary.

We can also update the values of pre-existing keys using this method.

The method can accept any number of arguments, which means it can append a dictionary with any number of {key: value} pairs at once. The value would be replaced with the new value even if the key is already present in the dictionary. A new key-value pair will be appended to the dictionary if the key doesn't already exist.

Example:

Code

Output:

{101: 'Ramya', 102: 'Sanya', 103: 'Sree'}
{101: 'Ramya', 102: 'Priyanka', 103: 'Ujjwala', 104: ' Sanya', 105: 'Sree'}

In the above example, the dictionary is modified 4 times using the update method. The first two statements updated the values of already existing keys, while the next two added two new pairs to the dictionary.

What if we want to add thousands of new key: value pairs?

We can't keep adding individual pairs one after the other. It takes a lot of time, making the code complex and lengthy.

The simple way is to create a new dictionary with all the new pairs we want to add and then append/ merge it into the dictionary.

Example:

Code

Output:

The original dictionary: 
{101: 'Ramya', 102: 'Sanya', 103: 'Sree'}
New key: value pairs: 
{104: 'Jeevani', 105: 'Rishitha', 106: 'Nikitha'}
The updated dictionary: 
{101: 'Ramya', 102: 'Sanya', 103: 'Sree', 104: 'Jeevani', 105: 'Rishitha', 106: 'Nikitha'}

3. OOPS Way:

We use the OOPS nature of Python to create a function capable of adding new key: value pairs into the dictionary.

The logic is the same traditional way, but the difference is we create an object for the dictionary, and the user can input the key: value pairs to add or modify the dictionary.

Example:

Code

Output:

#updating existing key:
{101: 'Ramya', 102: 'Sanya', 103: 'Sree'}
Enter the key you want to modify: 102
Enter the value to update: Priyanka
{101: 'Ramya', 102: 'Priyanka', 103: 'Sree'}

#adding new key:value pair:
{101: 'Ramya', 102: 'Sanya', 103: 'Sree'}
Enter the key you want to modify: 104
Enter the value to update: Sanya
{101: 'Ramya', 102: 'Sanya', 103: 'Sree', 104: 'Sanya'}

4. Dictionary Comprehension:

A single line of code in Python may be used to construct a dictionary, and the dictionary comprehension technique can be used to add a new key-value pair to an existing dictionary.

Here is an illustration of how to add a key-value combination using Python's dictionary comprehension feature:

Example:

Code

Output:

{'name': 'John', 'age': 30, 'address': '123 Main St.'}






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA