The basic way to add a single item to a dictionary
To add a new key-value pair to a Python dictionary, use square brackets with the key name and assign it a value. If the key does not exist, Python creates it. If the key already exists, Python overwrites the old value with the new one.
Here is the simplest example:
my_dict = {"name": "Alice", "age": 30} my_dict["city"] = "New York" print(my_dict) Output: {"name": "Alice", "age": 30, "city": "New York"}
The line my_dict["city"] = "New York" adds a new key called "city" with the value "New York". You can add as many items this way as you need, one at a time.
Key Takeaways
- Use square brackets and assignment to add a single item: my_dict["key"] = value
- The update() method adds multiple items at once from another dictionary or a list of pairs.
- The setdefault() method adds a key only if it does not already exist in the dictionary.
- Overwriting happens automatically when you assign to a key that already exists, so check first if you need to preserve the old value.
Adding multiple items at once with update()
When you need to add several key-value pairs in one step, use the update() method. Pass it another dictionary, and it merges all the pairs into your original dictionary.
my_dict = {"name": "Alice", "age": 30} new_data = {"city": "New York", "job": "Engineer"} my_dict.update(new_data) print(my_dict) Output: {"name": "Alice", "age": 30, "city": "New York", "job": "Engineer"}
You can also pass update() a list of tuples (pairs) instead of a dictionary. Each tuple should have two items: the key and the value.
my_dict = {"name": "Alice"} my_dict.update([("age", 30), ("city", "New York")]) print(my_dict) Output: {"name": "Alice", "age": 30, "city": "New York"}
Using setdefault() to add only if the key is missing
The setdefault() method adds a key-value pair only if the key does not already exist. If the key exists, it returns the current value and does nothing.
my_dict = {"name": "Alice", "age": 30} my_dict.setdefault("age", 25) print(my_dict) Output: {"name": "Alice", "age": 30}
In this example, "age" already exists with the value 30, so setdefault() does not change it. Now try adding a new key:
my_dict.setdefault("city", "New York") print(my_dict) Output: {"name": "Alice", "age": 30, "city": "New York"}
This is useful when you want to set a default value for a key without accidentally overwriting data that is already there.
What happens when you add to a key that already exists
If you assign a value to a key that is already in the dictionary, the old value is replaced immediately. Python does not warn you or keep the old value.
my_dict = {"name": "Alice", "age": 30} my_dict["age"] = 31 print(my_dict) Output: {"name": "Alice", "age": 31}
If you need to keep the old value or check whether a key exists before overwriting it, test for the key first using the in operator:
if "age" not in my_dict: my_dict["age"] = 31
This code only adds or changes "age" if it is not already in the dictionary.
Adding items from user input or loops
You often need to add items to a dictionary inside a loop or based on user input. The same square-bracket syntax works anywhere.
Here is an example that builds a dictionary from user input:
my_dict = {} for i in range(3): key = input("Enter a key: ") value = input("Enter a value: ") my_dict[key] = value print(my_dict)
Each time through the loop, the code reads a key and value from the user and adds them to the dictionary. You can also build a dictionary from a list of items by looping through and assigning each one.
Common mistakes when adding to dictionaries
One frequent mistake is forgetting the square brackets. Writing my_dict.age = 30 looks like it should work, but it does not add to the dictionary — it creates an attribute on the dictionary object itself, which is not what you want. Always use square brackets: my_dict["age"] = 30.
Another mistake is assuming update() returns the updated dictionary. It does not — it changes the dictionary in place and returns None. Do not write my_dict = my_dict.update(new_data) because this sets my_dict to None. Just write my_dict.update(new_data).
A third mistake is using a mutable value (like a list) as a key. Dictionary keys must be immutable — strings, numbers, and tuples are safe, but lists and dictionaries are not. If you try my_dict[[1, 2]] = "value", Python raises a TypeError.
Frequently Asked Questions
Can I add a list or another dictionary as a value in a dictionary?
Yes. Values can be any type of data, including lists, other dictionaries, numbers, strings, and tuples. Keys must be immutable, but values have no restriction. For example: my_dict["hobbies"] = ["reading", "coding"] or my_dict["address"] = {"city": "New York", "zip": "10001"}.
What is the difference between update() and adding items one at a time?
update() is faster and cleaner when you have many items to add at once, especially if they come from another dictionary. Adding one at a time is fine for a few items and gives you more control over each addition.
Does adding an item to a dictionary change its order?
In Python 3.7 and later, dictionaries keep items in the order they were added. New items appear at the end. In earlier Python versions, dictionary order was not may provide.
How do I add an item only if a condition is true?
Use an if statement before adding. For example: if age > 18: my_dict["adult"] = True. Or use setdefault() if you want to add a default value only when a key is missing.
Can I add multiple dictionaries together?
Yes, with update(). You can also use the merge operator | in Python 3.9 and later: combined = dict1 | dict2 creates a new dictionary with items from both, with dict2 overwriting any matching keys from dict1.