The basic way to add a single item to a dictionary
To add a new item to a dictionary in Python, 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 replaces the old value with the new one.
Here is the simplest example. If you have a dictionary called person and you want to add an age:
person = {"name": "Alice"} person["age"] = 30 print(person) Output: {"name": "Alice", "age": 30}
That is all you need for a single item. The key goes in the square brackets, and the value goes after the equals sign. Python does not care whether the key was there before — it will add it or overwrite it either way.
Key Takeaways
- Use square brackets and an equals sign to add a single item: dictionary[key] = value.
- The update() method adds multiple items at once and overwrites any keys that already exist.
- The setdefault() method adds a key only if it is not already in the dictionary.
- Dictionaries can hold any data type as a value, including lists, other dictionaries, or numbers.
Adding multiple items at once with update()
When you need to add more than one item, the update() method is faster than writing separate lines. Pass it another dictionary, and it adds all those key-value pairs to your original dictionary.
person = {"name": "Alice"} person.update({"age": 30, "city": "Portland"}) print(person) Output: {"name": "Alice", "age": 30, "city": "Portland"}
If a key in the update already exists in your dictionary, update() replaces the old value. This is useful when you want to change multiple items at once or merge two dictionaries together.
Adding a key only if it does not already exist
Sometimes you want to add a key, but only if it is not there already. The setdefault() method does this. It adds the key with a default value if the key is missing, and does nothing if the key already exists.
person = {"name": "Alice", "age": 30} person.setdefault("age", 25) print(person["age"]) Output: 30
In this example, the age key already exists with the value 30, so setdefault() leaves it alone. If you used setdefault() on a key that did not exist, it would add that key with the value you provided.
Storing different types of values in a dictionary
Dictionary values can be anything — strings, numbers, lists, or even other dictionaries. This flexibility makes dictionaries useful for storing complex information.
student = {} student["name"] = "Bob" student["grades"] = [85, 90, 78] student["address"] = {"street": "123 Main", "city": "Seattle"} print(student) Output: {"name": "Bob", "grades": [85, 90, 78], "address": {"street": "123 Main", "city": "Seattle"}}
You access nested values the same way you add them. To get Bob's city, you would write student["address"]["city"]. This nesting lets you organize related information together.
What happens when you add to a dictionary inside a loop
Adding items inside a loop is common when you are building a dictionary from data. Each time the loop runs, you add a new key-value pair based on the current data.
scores = {} names = ["Alice", "Bob", "Carol"] points = [95, 87, 92] for i in range(len(names)): scores[names[i]] = points[i] print(scores) Output: {"Alice": 95, "Bob": 87, "Carol": 92}
This pattern is useful when you have data in separate lists or when you are reading data from a file or database. Each iteration adds one new entry to the dictionary.
Comparing the three methods side by side
| Method | What It Does | When to Use It |
|---|---|---|
| Square brackets: dict[key] = value | Adds or replaces one item | Adding a single item or changing an existing value |
| update() | Adds or replaces multiple items at once | Adding many items or merging two dictionaries |
| setdefault() | Adds an item only if the key does not exist | Setting a default value without overwriting existing data |
Frequently Asked Questions
Can I add a key that has spaces or special characters?
Yes. Any string can be a key, including ones with spaces or symbols. Just put the key in quotes: dictionary["first name"] = "Alice" or dictionary["@email"] = "test@example.com". You access it the same way you added it.
What if I try to add to something that is not a dictionary?
Python will give you an error. Lists use square brackets too, but they only accept numbers as positions, not string keys. If you get a TypeError, check that you are working with a dictionary, not a list or string.
Does adding items to a dictionary change the order they are stored in?
In Python 3.7 and later, dictionaries keep items in the order you added them. Older versions did not may provide this. If you need a specific order, you can sort the dictionary keys when you print or loop through it.
Can I use a number as a dictionary key?
Yes. Numbers work as keys just like strings do: ages = {1: "Alice", 2: "Bob"}. You access them without quotes: ages[1] returns "Alice". Be careful not to confuse a number key with a list position.