The three ways to add to a dictionary

You add items to a Python dictionary using square bracket notation, the update() method, or the setdefault() method. The square bracket approach is the most common: you write the dictionary name, then the key in brackets, then assign a value. The other two methods handle multiple items or conditional additions.

Which one you choose depends on whether you're adding a single item, multiple items at once, or only adding a key if it doesn't already exist. All three modify the dictionary in place — they don't create a new dictionary.

Key Takeaways

  • Square bracket notation (my_dict[key] = value) is the standard way to add or change a single item in a dictionary.
  • The update() method adds multiple key-value pairs at once and overwrites any keys that already exist.
  • The setdefault() method adds a key only if it is not already in the dictionary, leaving existing values unchanged.
  • All three methods change the original dictionary — they do not return a new one.

Adding a single item with square brackets

The square bracket method is what you'll use most often. Write the dictionary name, then the key inside square brackets, then use the equals sign to assign a value:

my_dict = {"name": "Alice", "age": 30} my_dict["city"] = "Portland" print(my_dict) # Output: {"name": "Alice", "age": 30, "city": "Portland"}

If the key already exists, this method overwrites the old value. So if you run my_dict["age"] = 31, the age changes from 30 to 31. This works the same way whether the dictionary is empty or already has items.

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 (or a list of key-value pairs) inside the parentheses:

my_dict = {"name": "Alice", "age": 30} my_dict.update({"city": "Portland", "job": "Engineer"}) print(my_dict) # Output: {"name": "Alice", "age": 30, "city": "Portland", "job": "Engineer"}

The update() method also overwrites any keys that already exist. If you pass {"age": 31} to update, the age will change. This is useful when you're merging data from two sources or loading a batch of settings at once.

Adding only if the key doesn't exist with setdefault()

The setdefault() method adds a key-value pair only if the key is not already in the dictionary. If the key exists, it leaves the value alone:

my_dict = {"name": "Alice", "age": 30} my_dict.setdefault("age", 25) print(my_dict) # Output: {"name": "Alice", "age": 30}

In this example, age stays 30 because the key already exists. If you use setdefault("city", "Portland") instead, it adds the city because that key is not in the dictionary. This method is helpful when you want to set a default value without overwriting data that's already there.

Comparing the three methods

MethodUse forOverwrites existing keys?Example
Square bracketsSingle itemYesmy_dict["key"] = "value"
update()Multiple itemsYesmy_dict.update({"key": "value"})
setdefault()Conditional additionNomy_dict.setdefault("key", "value")

Common mistakes when adding to dictionaries

A frequent error is forgetting that dictionaries use curly braces, not parentheses. If you write my_dict("key") = "value", Python will throw an error because it thinks you're trying to call the dictionary as a function. Always use square brackets: my_dict["key"] = "value".

Another mistake is assuming that update() returns a new dictionary. It doesn't — it modifies the original one and returns None. So if you write new_dict = my_dict.update({"key": "value"}), new_dict will be None, not a dictionary with the new items. Instead, call update on the dictionary you want to change, then use that same dictionary afterward.

Frequently Asked Questions

Can I add a key with a list or another dictionary as the value?

Yes. The value can be any Python object — a string, number, list, dictionary, or anything else. For example, my_dict["hobbies"] = ["reading", "coding"] or my_dict["address"] = {"street": "Main St", "city": "Portland"} both work fine.

What happens if I add the same key twice?

The second assignment overwrites the first. If you run my_dict["name"] = "Alice" and then my_dict["name"] = "Bob", the name will be "Bob". Dictionaries cannot have duplicate keys — each key appears only once.

Does update() work with lists of tuples?

Yes. You can pass update() a list of tuples where each tuple is a key-value pair: my_dict.update([("name", "Alice"), ("age", 30)]). This is useful when you're reading data from a file or another source that gives you pairs instead of a dictionary.

Can I add items to a dictionary inside a loop?

Yes, and it's a common pattern. For example, you can loop through a list and add each item to a dictionary with a key you create: for i, item in enumerate(items): my_dict[i] = item. The dictionary grows with each iteration.