Adding items to a dictionary is done with square brackets and an equals sign

A dictionary in Python is a container that stores pairs of information — a key and a value. Think of it like a real dictionary where the word is the key and the definition is the value. To add a new pair to an existing dictionary, you write the dictionary name, then square brackets with the key inside, then an equals sign, then the value you want to store.

Here is the basic pattern:

my_dict[key] = value

If the key already exists in the dictionary, this line will replace the old value with the new one. If the key does not exist, Python creates it and adds the pair to the dictionary. This is the simplest and most common way to add items.

Key Takeaways

  • Use square brackets and an equals sign to add a single item: my_dict["name"] = "Alice"
  • If the key already exists, the new value overwrites the old one instead of creating a duplicate.
  • The update() method lets you add multiple items at once from another dictionary or a list of pairs.
  • Keys must be immutable types like strings or numbers, but values can be any type including lists, other dictionaries, or numbers.
  • You can add items inside a loop to build a dictionary gradually as you process data.

Adding a single item with square bracket notation

The most direct way to add one item is to reference the dictionary by name, put the key in square brackets, and assign the value with an equals sign. Here is a concrete example:

Suppose you have a dictionary that stores information about a person:

person = {"name": "Alice", "age": 30}

To add their city, you write:

person["city"] = "Portland"

Now the dictionary contains three pairs: name, age, and city. If you later write person["age"] = 31, the age value changes from 30 to 31, but no duplicate key is created. Python always keeps one value per key.

Adding multiple items at once with the update() method

When you need to add several items, the update() method is faster than writing separate lines. You pass it another dictionary or a list of key-value pairs, and it adds all of them to your original dictionary.

Using another dictionary:

person.update({"email": "alice@example.com", "phone": "555-1234"})

Using a list of pairs (called tuples):

person.update([("job", "Engineer"), ("years_experience", 8)])

Both approaches add the new keys to the dictionary. If any key already exists, update() replaces its value, just like the square bracket method does. The difference is that update() handles multiple items in one call, which is cleaner when you have many to add.

Adding items inside a loop

You often build a dictionary gradually by looping through data and adding items as you go. For example, if you have a list of names and want to create a dictionary where each name is a key and its length is the value:

names = ["Alice", "Bob", "Charlie"]name_lengths = {}for name in names:  name_lengths[name] = len(name)

After this loop runs, name_lengths contains {"Alice": 5, "Bob": 3, "Charlie": 7}. Each time through the loop, the square bracket notation adds a new key-value pair. This pattern is common when you are processing a list or reading from a file and need to organize the results into a dictionary.

Understanding keys and values

Keys must be immutable, meaning they cannot be changed after creation. Strings and numbers work as keys. Lists do not, because lists can be modified. Values, on the other hand, can be anything — strings, numbers, lists, other dictionaries, or even functions.

For example, this is valid:

student = {}student["name"] = "Alice"student["grades"] = [95, 87, 92]student["address"] = {"street": "123 Main", "city": "Portland"}

The key "name" is a string, "grades" is a string, and "address" is a string — all immutable. But the values are a string, a list, and another dictionary. Python allows this flexibility because values do not need to be unique or serve as lookups the way keys do.

What happens when you add to a dictionary that already has the key

If you add an item using a key that already exists, the old value is replaced silently. Python does not warn you or create a second entry. This is by design — dictionaries enforce one value per key.

If you need to keep the old value and add a new one, you have to store them differently. One approach is to make the value a list and append to it:

scores = {"Alice": [95, 87]}scores["Alice"].append(92)

Now Alice's value is a list with three scores. Another approach is to use a different key, like adding a number to the end: scores["Alice_2"] = 92. The right choice depends on what your data represents and how you plan to use it later.

Frequently Asked Questions

Can I add a key that is a number instead of a string?

Yes. Numbers are immutable, so they work as keys. You can write my_dict[1] = "first" or my_dict[3.14] = "pi". Be careful though — my_dict[1] and my_dict["1"] are different keys, because one is a number and one is a string.

What is the difference between adding with square brackets and using update()?

Square brackets add or replace one item at a time. The update() method adds or replaces multiple items in one call. Both do the same thing to the dictionary — they just offer different convenience depending on how many items you are adding.

Can I add an item with a key that contains spaces or special characters?

Yes, as long as the key is a string. You can write my_dict["first name"] = "Alice" or my_dict["email@domain"] = "test". However, you cannot use dot notation like my_dict.first name — you must use square brackets when the key has spaces or special characters.

What happens if I try to add a list as a key?

Python will raise an error because lists are mutable and cannot be keys. If you need a key made of multiple values, use a tuple instead: my_dict[(1, 2)] = "value". Tuples are immutable and work as keys.

Can I add items to a dictionary while looping through it?

You can add items, but modifying a dictionary while looping through it can cause unexpected behavior. The safer approach is to loop through a copy or a list of keys, or to collect the new items and add them after the loop finishes.