Last modified: Jan 27, 2026 By Alexander Williams

Python Add to Dict: Methods and Examples

Python dictionaries store data in key-value pairs. They are fundamental and flexible. You will often need to add new data to them. This guide explains all the methods to add items to a Python dictionary.

We will cover basic assignment, updating multiple items, and modern merging techniques. Each method has its use case. Understanding them makes your code cleaner and more efficient.

Using Square Bracket Assignment

The simplest way to add a single item is with square brackets. You specify a new key and assign a value to it. If the key already exists, its value is overwritten.

This is the most common method for adding or modifying a single entry. It is direct and easy to read.


# Create an empty dictionary
my_dict = {}

# Add a new key-value pair
my_dict["name"] = "Alice"
print(my_dict)

# Add another pair
my_dict["age"] = 30
print(my_dict)

# Update the value of an existing key
my_dict["age"] = 31
print(my_dict)
    

{'name': 'Alice'}
{'name': 'Alice', 'age': 30}
{'name': 'Alice', 'age': 31}
    

Using the update() Method

The update() method is powerful. It can add multiple items at once. You can pass another dictionary, an iterable of key-value pairs, or keyword arguments.

This method is perfect for combining dictionaries or adding data from lists. It modifies the original dictionary in place.


# Start with a dictionary
inventory = {"apples": 5, "bananas": 2}

# Add items from another dictionary
inventory.update({"oranges": 7, "grapes": 12})
print("After update with dict:", inventory)

# Add items from a list of tuples
new_items = [("pears", 4), ("kiwis", 6)]
inventory.update(new_items)
print("After update with list:", inventory)

# Add items using keyword arguments
inventory.update(mangoes=3, peaches=9)
print("After update with keywords:", inventory)
    

After update with dict: {'apples': 5, 'bananas': 2, 'oranges': 7, 'grapes': 12}
After update with list: {'apples': 5, 'bananas': 2, 'oranges': 7, 'grapes': 12, 'pears': 4, 'kiwis': 6}
After update with keywords: {'apples': 5, 'bananas': 2, 'oranges': 7, 'grapes': 12, 'pears': 4, 'kiwis': 6, 'mangoes': 3, 'peaches': 9}
    

If you need to create a dictionary from separate lists, check out our guide on how to Import Lists into Python Dictionary.

Merging with the | Operator (Python 3.9+)

Python 3.9 introduced the merge | operator. It creates a new dictionary by merging two dictionaries. The |= update operator merges in place.

This provides a clean, readable syntax for dictionary union operations. It is now the preferred way for many developers.


# Two dictionaries to merge
default_settings = {"theme": "light", "notifications": True}
user_settings = {"notifications": False, "language": "EN"}

# Merge into a new dictionary (| operator)
merged_settings = default_settings | user_settings
print("Merged dictionary:", merged_settings)

# Update in place (|= operator)
default_settings |= user_settings
print("Updated default_settings:", default_settings)
    

Merged dictionary: {'theme': 'light', 'notifications': False, 'language': 'EN'}
Updated default_settings: {'theme': 'light', 'notifications': False, 'language': 'EN'}
    

Important: When keys conflict, the value from the right-most dictionary wins. This is true for both the | operator and the update() method.

Adding Items Conditionally

Sometimes you only want to add a key if it doesn't already exist. The setdefault() method is useful here. It inserts a key with a default value only if the key is absent.

For more control, you can use simple if checks or the get() method. This prevents accidentally overwriting existing data.


data = {"product": "Laptop", "price": 999}

# Using setdefault() to add a missing key
data.setdefault("in_stock", True)
print("After setdefault (new key):", data)

# setdefault does nothing if key exists
data.setdefault("price", 799) # Price remains 999
print("After setdefault (existing key):", data)

# Conditional add with an if statement
if "warranty" not in data:
    data["warranty"] = "2 years"
print("After conditional add:", data)
    

After setdefault (new key): {'product': 'Laptop', 'price': 999, 'in_stock': True}
After setdefault (existing key): {'product': 'Laptop', 'price': 999, 'in_stock': True}
After conditional add: {'product': 'Laptop', 'price': 999, 'in_stock': True, 'warranty': '2 years'}
    

Adding Items from Iteration

You often build dictionaries dynamically from loops. Start with an empty dict. Then add key-value pairs inside the loop.

This pattern is common in data processing. For example, counting items or mapping results.


# Example: Create a dictionary of squares
squares = {}
for num in range(1, 6):
    squares[num] = num ** 2  # Add key-value pair in loop
print("Squares dictionary:", squares)

# Example: Count frequency of letters
word = "mississippi"
frequency = {}
for letter in word:
    # Use get() to handle first occurrence
    frequency[letter] = frequency.get(letter, 0) + 1
print("Letter frequency:", frequency)
    

Squares dictionary: {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
Letter frequency: {'m': 1, 'i': 4, 's': 4, 'p': 2}
    

If you need to remove items while iterating or processing, understand methods like Python Dict Pop to do it safely.

Common Errors and Best Practices

Adding to dictionaries is straightforward. But you should avoid common pitfalls.

Do not confuse dictionary methods with list methods. A common error is trying to use append() or sort() on a dict. Learn how to Fix Python Dict Has No Attribute Sort Error if you encounter it.

Best Practices:

  • Use square brackets for single, simple additions.
  • Use update() for merging multiple items from another mapping.
  • Use the | operator (Python 3.9+) for clean merging into a new dictionary.
  • Use setdefault() or checks to avoid overwriting existing keys unintentionally.
  • Remember that as of Python 3.7, dictionaries maintain insertion order.

For more on dictionary order, read Is Python Dict in Order of Append?.

Conclusion

Adding items to a Python dictionary is a core skill. You have several tools: square bracket assignment, the update() method, and the merge | operator.

Choose the method based on your task. For one item, use brackets. For many items, use update() or the merge operator. To add conditionally, use setdefault() or an if check.

Master these techniques. They will help you manage data effectively in your Python programs. Dictionaries are versatile. Knowing how to populate them is key to unlocking their power.