Last modified: May 23, 2026
Python Dictionary Append: A Simple Guide
Many beginners ask if Python dictionaries have an append method. The answer is no. Lists have append, but dictionaries don't. Dictionaries store data as key-value pairs. You add new items by assigning a value to a new key. This is the core concept of dictionary "appending".
In this article, you will learn several ways to add items to a dictionary. We will cover basic key assignment, the update method, and the setdefault method. Each method has its own use case. You will see code examples and outputs for each technique.
What is a Python Dictionary?
A dictionary is a mutable data structure. It stores data in key-value pairs. Keys must be unique and immutable. Values can be of any type. Dictionaries are unordered in Python versions before 3.7. In Python 3.7+, they maintain insertion order.
Think of a dictionary like a real dictionary. You look up a word (key) to find its definition (value). This makes dictionaries very fast for lookups. They are ideal for mapping relationships.
Method 1: Direct Key Assignment
This is the most common way to add a new item. You simply assign a value to a new key. If the key already exists, the value is updated. If not, a new key-value pair is added.
Here is the syntax: dictionary[key] = value. It is simple and efficient. This method is best when you know the exact key you want to add.
# Create an empty dictionary
student_grades = {}
# Append a new key-value pair
student_grades["Alice"] = 85
student_grades["Bob"] = 92
student_grades["Charlie"] = 78
# Display the dictionary
print(student_grades)
{'Alice': 85, 'Bob': 92, 'Charlie': 78}
Notice how we added three students. Each assignment appended a new entry. If you try to add the same key again, the old value is overwritten.
# Overwrite an existing key
student_grades["Alice"] = 90
print(student_grades)
{'Alice': 90, 'Bob': 92, 'Charlie': 78}
Method 2: Using the update Method
The update method adds multiple key-value pairs at once. It can take another dictionary or an iterable of key-value pairs. This method is useful when you have a batch of data to add.
Here is the syntax: dictionary.update({key: value, ...}). It modifies the original dictionary in place. It returns None.
# Create a dictionary
inventory = {"apples": 10, "bananas": 5}
# Append multiple items using update
inventory.update({"oranges": 8, "grapes": 12})
print(inventory)
{'apples': 10, 'bananas': 5, 'oranges': 8, 'grapes': 12}
You can also use update with a list of tuples. This is helpful when data comes from a different source.
# Append using a list of tuples
new_items = [("pears", 7), ("kiwis", 4)]
inventory.update(new_items)
print(inventory)
{'apples': 10, 'bananas': 5, 'oranges': 8, 'grapes': 12, 'pears': 7, 'kiwis': 4}
Method 3: Using the setdefault Method
The setdefault method adds a key only if it does not exist. If the key exists, it returns the current value. If not, it inserts the key with the default value and returns that value. This prevents accidental overwrites.
Here is the syntax: dictionary.setdefault(key, default_value). The default value is optional. If omitted, it defaults to None.
# Start with a dictionary
user_settings = {"theme": "dark"}
# Append with setdefault
theme = user_settings.setdefault("theme", "light")
print(f"Current theme: {theme}")
# Append a new key
language = user_settings.setdefault("language", "English")
print(f"Language setting: {language}")
print(user_settings)
Current theme: dark
Language setting: English
{'theme': 'dark', 'language': 'English'}
Notice that the "theme" key was not changed. The "language" key was added because it was missing. This method is great for setting defaults.
Common Mistakes to Avoid
Do not try to use the list append method on a dictionary. It will raise an AttributeError. Dictionaries do not have append.
# This will cause an error
my_dict = {"a": 1}
# my_dict.append("b", 2) # Uncomment to see the error
Another mistake is forgetting that keys must be immutable. You cannot use lists or other dictionaries as keys. Use strings, numbers, or tuples instead.
If you want to add a value to a list inside a dictionary, you can use append on that list. For example: my_dict["key"].append(value). This is a common pattern when building lists of values.
Practical Example: Building a Dictionary Dynamically
Let's build a dictionary from user input. This shows how to append items in a loop. It is a common task in real programs.
# Build a dictionary of contacts
contacts = {}
# Simulate user input
data = [
("Alice", "[email protected]"),
("Bob", "[email protected]"),
("Charlie", "[email protected]"),
]
for name, email in data:
contacts[name] = email # Append each contact
print(contacts)
{'Alice': '[email protected]', 'Bob': '[email protected]', 'Charlie': '[email protected]'}
This pattern is very powerful. You can use it to process data from files, APIs, or databases. The dictionary grows as you add more items.
When to Use Each Method
Use direct key assignment when you add one item at a time. It is the fastest and most readable.
Use the update method when you have multiple items to add at once. It keeps your code clean.
Use the setdefault method when you want to add a default value only if the key is missing. It prevents accidental overwrites.
For more on list operations, check out our guide on Python List Operations Guide for Beginners. It covers similar concepts for lists.
Performance Considerations
Dictionary append operations are very fast. They have an average time complexity of O(1). Adding a new key-value pair is almost instantaneous, even for large dictionaries.
However, be careful with large updates. The update method iterates over all items in the input. For very large batches, it may take some time. But for most use cases, performance is not an issue.
If you need to remove items later, learn about Python List Remove by Value. While it focuses on lists, the concepts of removing data are similar.
Conclusion
Python dictionaries do not have an append method. Instead, you add items using key assignment. This is simple and efficient. You have learned three main ways to add items: direct assignment, update, and setdefault. Each method has its own strengths.
Practice with these examples to build confidence. Dictionaries are a fundamental part of Python. Mastering them will make your code cleaner and faster. For more advanced dictionary operations, explore the official Python documentation.
Remember, the key to learning is practice. Try modifying the examples above. Add your own data. Experiment with different methods. Soon, dictionary appending will become second nature.
If you work with lists too, our article on Python List Extend: A Complete Guide can help you understand how lists handle similar tasks.