Python: How to Add Items to Dictionary

July 4, 2024

Introduction

Python dictionaries are a built-in data type for storing key-value pairs. The dictionary elements are mutable and don't allow duplicates. Adding a new element appends it to the end, and in Python 3.7+, the elements are ordered.

Depending on the desired result and given data, there are various ways to add items to a dictionary.

This guide shows how to add items to a Python dictionary through examples.

Python: How to Add Items to Dictionary

Prerequisites

How to Add an Item to a Dictionary in Python

Create an example dictionary to test different ways to add items to a dictionary. For example, initialize a dictionary with two items:

my_dictionary = {
  "one": 1,
  "two": 2
}
print(my_dictionary)
example dictionary terminal output

The methods below show how to add one or more items to the example dictionary.

Note: Adding an item with an existing key replaces the dictionary item with a new value. Provide unique keys to avoid overwriting data.

Method 1: Using The Assignment Operator

The assignment operator (=) sets a value to a dictionary key:

dictionary_name[key] = value

The assignment operator adds a new key-value pair if the key does not exist. For example:

my_dictionary = {
  "one": 1,
  "two": 2
}
my_dictionary["three"] = 3
print(my_dictionary)
Python dictionary assignment operator

The code shows the updated dictionary contents after adding a new item.

Method 2: Using update()

The update() method adds a new element to an existing dictionary:

dictionary_name.update({key:value})

The method also accepts multiple key-value pairs. To use the update() method, see the example below:

my_dictionary = {
  "one": 1,
  "two": 2
}
my_dictionary.update({"three":3})
print(my_dictionary)
Python dictionary add item update output

Use this method to add new items or to append a dictionary to an existing one.

Method 3: Using dict() Constructor

The dict() constructor allows creating a new dictionary and adding a value to an existing one. When using the second approach, the method creates a copy of a dictionary and appends an element to it.

The syntax is:

new_dictionary = dict(old_dictionary, key=value)

For example:

my_dictionary = {
  "one": 1,
  "two": 2
}
new_dictionary = dict(my_dictionary, three=3)
print(new_dictionary)
Python dictionary add item dict() method output

The method preserves the original dictionary and updates the new copy.

Method 4: Using __setitem__

The __setitem__ method is another way to add an item to a dictionary. The syntax is:

dictionary_name.__setitem__(key,value)

For example:

my_dictionary = {
  "one": 1,
  "two": 2
}
my_dictionary.__setitem__("three", 3)
print(my_dictionary)
Python dictionary add item __setitem__ method output

The method sets the item key as "three" with the value 3.

Method 5: Using The ** Operator

The ** operator merges an existing dictionary into a new dictionary and enables adding additional items. The syntax is:

new_dictionary = {**old_dicitonary, **{key:value}}

For example, to copy an existing dictionary and append a new item, see the following code:

my_dictionary = {
  "one": 1,
  "two": 2
}
new_dictionary = {**my_dictionary, **{"three":3}}
print(new_dictionary)
Python dictionary add item asterisk operator output

The new dictionary contains the added item while preserving the old dictionary. This method avoids changing dictionaries and creates a copy for changes instead.

Method 6: Checking If A Key Exists

To avoid overwriting existing data, use an if statement to check whether a key is present before adding a new item to a dictionary. The example syntax is:

if key not in dictionary_name:
    dictionary_name[key] = value

For example:

my_dictionary = {
  "one": 1,
  "two": 2
}
if "three" not in my_dictionary:
  my_dictionary["three"] = 3
print(my_dictionary)
Python dictionary add item check if exists output

The code checks whether a key with the provided name exists. If the provided key exists, the existing key value does not update, and the dictionary stays unchanged.

Method 7: Using A For Loop

Add key-value pairs to a nested list and loop through the list to add multiple items to a dictionary. For example:

my_dictionary = {
  "one": 1,
  "two": 2
}
my_list = [["three", 3], ["four", 4]]
for key,value in my_list:
    my_dictionary[key] = value
print(my_dictionary)
Python dictionary add item for loop output

The for loop goes through the pairs inside the list and adds two new elements.

Note: A for loop and a counter are also used to identify the length of a list. Learn more by reading our guide How to Find the List Length in Python.

Method 8: Using zip

Use zip to create dictionary items from two lists. The first list contains keys, and the second contains the values.

For example:

my_dictionary = {
  "one": 1,
  "two": 2
}
my_keys = ["three", "four"]
my_values = [3, 4]
for key,value in zip(my_keys, my_values):
    my_dictionary[key] = value
print(my_dictionary)
Python dictionary add item zip output

The zip function matches elements from two lists by index, creating key-value pairs.

Conclusion

This guide showed how to add items to a Python dictionary. All methods provide unique functionalities, so choose the one that best suits your program and situation.

For more Python tutorials, refer to our article on how to pretty print a JSON file using Python or learn about Python dictionary comprehension.

Was this article helpful?
YesNo
Milica Dancuk
Milica Dancuk is a technical writer at phoenixNAP with a passion for programming. With a background in Electrical Engineering and Computing, coupled with her teaching experience, she excels at simplifying complex technical concepts in her writing.
Next you should read
File Handling in Python: Create, Open, Append, Read, Write
February 24, 2022

Working with files is part of everyday tasks in programming. This tutorial teaches you elementary file...
Read more
Handling Missing Data in Python: Causes and Solutions
July 1, 2021

Some machine learning algorithms won't work with missing data. Learn how to discover if your dataset has missing...
Read more
How to Comment in Python
November 25, 2019

The ability to use comments while writing code is an important skill valued among developers. These comments can be used to leave notes about the...
Read more
How to Get the Current Date and Time in Python
March 12, 2024

The article shows you how to create a basic Python script that displays the current date and time. Find out how to use the...
Read more