Adding Dictionary Items in Python: Inserting Key-Value Pairs

thumb_up 1  ·  sell Python dictionary item addition, Inserting key-value pairs in Python dictionaries, Adding items to a Python dictionary

Using the Operator

The "[]" operator (used to access value mapped to a dictionary key) is used to update an existing key-value pair as well as add a new pair.

Syntax

dict["key"] = val

If the key is already present in the dictionary object, its value will be updated to val. If the key is not present in the dictionary, a new key-value pair will be added.

Example

In this example, the marks of "Laxman" are updated to 95.

 
marks = {"Savita":67, "Imtiaz":88, "Laxman":91, "David":49} print ("marks dictionary before update: ", marks) marks['Laxman'] = 95 print ("marks dictionary after update: ", marks)

It will produce the following output −

marks dictionary before update: {'Savita': 67, 'Imtiaz': 88, 'Laxman': 91, 'David': 49}
marks dictionary after update: {'Savita': 67, 'Imtiaz': 88, 'Laxman': 95, 'David': 49}

Example

However, an item with 'Krishnan' as its key is not available in the dictionary, hence a new key-value pair is added.

 
marks = {"Savita":67, "Imtiaz":88, "Laxman":91, "David":49} print ("marks dictionary before update: ", marks) marks['Krishan'] = 74 print ("marks dictionary after update: ", marks)

It will produce the following output −

marks dictionary before update: {'Savita': 67, 'Imtiaz': 88, 'Laxman': 91, 'David': 49}
marks dictionary after update: {'Savita': 67, 'Imtiaz': 88, 'Laxman': 91, 'David': 49, 'Krishan': 74}

Using the update() Method

You can use the update() method in dict class in three different ways:

Update with Another Dictionary

In this case, the update() method's argument is another dictionary. Value of keys common in both dictionaries is updated. For new keys, key-value pair is added in the existing dictionary

Syntax

d1.update(d2)

Return value

The existing dictionary is updated with new key-value pairs added to it.

Example

 
marks = {"Savita":67, "Imtiaz":88, "Laxman":91, "David":49} print ("marks dictionary before update: \n", marks) marks1 = {"Sharad": 51, "Mushtaq": 61, "Laxman": 89} marks.update(marks1) print ("marks dictionary after update: \n", marks)

It will produce the following output −

marks dictionary before update:
{'Savita': 67, 'Imtiaz': 88, 'Laxman': 91, 'David': 49}
marks dictionary after update:
{'Savita': 67, 'Imtiaz': 88, 'Laxman': 89, 'David': 49, 'Sharad': 51, 'Mushtaq': 61}

Update with Iterable

If the argument to update() method is a list or tuple of two item tuples, an item each for it is added in the existing dictionary, or updated if the key is existing.

Syntax

d1.update([(k1, v1), (k2, v2)])

Return value

Existing dictionary is updated with new keys added.

Example

 
marks = {"Savita":67, "Imtiaz":88, "Laxman":91, "David":49} print ("marks dictionary before update: \n", marks) marks1 = [("Sharad", 51), ("Mushtaq", 61), ("Laxman", 89)] marks.update(marks1) print ("marks dictionary after update: \n", marks)

It will produce the following output −

marks dictionary before update:
 {'Savita': 67, 'Imtiaz': 88, 'Laxman': 91, 'David': 49}
marks dictionary after update:
 {'Savita': 67, 'Imtiaz': 88, 'Laxman': 89, 'David': 49, 'Sharad': 51, 'Mushtaq': 61}

Update with Keyword Arguments

Third version of update() method accepts list of keyword arguments in name=value format. New k-v pairs are added, or value of existing key is updated.

Syntax

d1.update(k1=v1, k2=v2)

Return value

Existing dictionary is updated with new key-value pairs added.

Example

 
marks = {"Savita":67, "Imtiaz":88, "Laxman":91, "David":49} print ("marks dictionary before update: \n", marks) marks.update(Sharad = 51, Mushtaq = 61, Laxman = 89) print ("marks dictionary after update: \n", marks)

It will produce the following output −

marks dictionary before update:
 {'Savita': 67, 'Imtiaz': 88, 'Laxman': 91, 'David': 49}
marks dictionary after update:
 {'Savita': 67, 'Imtiaz': 88, 'Laxman': 89, 'David': 49, 'Sharad': 51, 'Mushtaq': 61}

Using the Unpack Operator

The "**" symbol prefixed to a dictionary object unpacks it to a list of tuples, each tuple with key and value. Two dict objects are unpacked and merged together and obtain a new dictionary.

Syntax

d3 = {**d1, **d2}

Return value

Two dictionaries are merged and a new object is returned.

Example

 
marks = {"Savita":67, "Imtiaz":88, "Laxman":91, "David":49} print ("marks dictionary before update: \n", marks) marks1 = {"Sharad": 51, "Mushtaq": 61, "Laxman": 89} newmarks = {**marks, **marks1} print ("marks dictionary after update: \n", newmarks)

It will produce the following output −

marks dictionary before update:
 {'Savita': 67, 'Imtiaz': 88, 'Laxman': 91, 'David': 49}
marks dictionary after update:
 {'Savita': 67, 'Imtiaz': 88, 'Laxman': 89, 'David': 49, 'Sharad': 51, 'Mushtaq': 61}

Using the Union Operator (|)

Python introduces the "|" (pipe symbol) as the union operator for dictionary operands. It updates existing keys in dict object on left, and adds new key-value pairs to return a new dict object.

Syntax

d3 = d1 | d2

Return value

The Union operator return a new dict object after merging the two dict operands

Example

 
marks = {"Savita":67, "Imtiaz":88, "Laxman":91, "David":49} print ("marks dictionary before update: \n", marks) marks1 = {"Sharad": 51, "Mushtaq": 61, "Laxman": 89} newmarks = marks | marks1 print ("marks dictionary after update: \n", newmarks)

It will produce the following output −

marks dictionary before update:
 {'Savita': 67, 'Imtiaz': 88, 'Laxman': 91, 'David': 49}
marks dictionary after update:
 {'Savita': 67, 'Imtiaz': 88, 'Laxman': 89, 'David': 49, 'Sharad': 51, 'Mushtaq': 61}

Using "|=" Operator

The "|=" operator is an augmented Union operator. It performs in-place update o n the dictionary operand on left by adding new keys in the operand on right, and updating the existing keys.

Syntax

d1 |= d2

Example

 
marks = {"Savita":67, "Imtiaz":88, "Laxman":91, "David":49} print ("marks dictionary before update: \n", marks) marks1 = {"Sharad": 51, "Mushtaq": 61, "Laxman": 89} marks |= marks1 print ("marks dictionary after update: \n", marks)

It will produce the following output −

marks dictionary before update:
 {'Savita': 67, 'Imtiaz': 88, 'Laxman': 91, 'David': 49}
marks dictionary after update:
 {'Savita': 67, 'Imtiaz': 88, 'Laxman': 89, 'David': 49, 'Sharad': 51



The End! should you have any inquiries, we encourage you to reach out to the Vercaa Support Center without hesitation.

Was this answer helpful?

Related Articles

description

Exploring Python's Key Characteristic

Python is a feature rich high-level, interpreted, interactive and object-oriented scripting language. This tutorial will list down some of…

arrow_forward
description

Comparing Python and C++

Both Python and C++ are among the most popular programming languages. Both of them have their advantages and disadvantages. In this…

arrow_forward
description

Creating a Python Hello World Program

This tutorial will teach you how to write a simple Hello World program using Python Programming language. This program will make use of…

arrow_forward
description

Python's Versatile Application Domains

Python is a general-purpose programming language. It is suitable for development of wide range of software applications. Over last few…

arrow_forward
description

Understanding the Python Interpreter

Python is an interpreter-based language. In a Linux system, Python's executable is installed in /usr/bin/ directory. For Windows, the…

arrow_forward
arrow_back « Back