How to Add a Key to a Dictionary in Python

If you want to add a new key to a Python dictionary, the simplest way is to assign a value with square brackets.

This page shows you how to:

  • Add one new key-value pair to a dictionary
  • Update a key if it already exists
  • Choose between bracket assignment and update()
  • Understand what happens when the key is already present

Quick answer

person = {"name": "Ana"}
person["age"] = 25

print(person)
# {'name': 'Ana', 'age': 25}

Use square brackets with a new key name. If the key already exists, the value is replaced.

What this page helps you do

  • Add one new key-value pair to a dictionary
  • Update a key if it already exists
  • Choose between bracket assignment and update()
  • Understand what happens when the key is already present

If you are new to dictionaries, see Python dictionaries explained first.

Add a key with square brackets

The most direct way to add a key is:

my_dict[key] = value

If the key does not exist, Python creates it.

If the key already exists, Python updates its value.

Example:

student = {"name": "Liam"}
student["grade"] = "A"

print(student)
# {'name': 'Liam', 'grade': 'A'}

In this example:

  • "grade" is the new key
  • "A" is the value stored for that key

You can also update an existing key the same way:

student = {"name": "Liam", "grade": "A"}
student["grade"] = "B"

print(student)
# {'name': 'Liam', 'grade': 'B'}

This does not create a second "grade" key. It replaces the old value.

If you need a refresher on creating dictionaries, see creating a dictionary in Python.

Add multiple keys with update()

Use update() when you want to add several items at once.

Example:

person = {"name": "Ana"}
person.update({"age": 25, "city": "Madrid"})

print(person)
# {'name': 'Ana', 'age': 25, 'city': 'Madrid'}

Here, update() takes another dictionary and adds its key-value pairs.

It is useful when:

  • You want to add several keys in one step
  • You already have another dictionary
  • You want a short way to merge small sets of values

Example with an existing key:

person = {"name": "Ana", "age": 20}
person.update({"age": 25, "city": "Madrid"})

print(person)
# {'name': 'Ana', 'age': 25, 'city': 'Madrid'}

Notice that "age" was overwritten.

If you want more detail, see the Python dictionary update() method.

What happens if the key already exists

A Python dictionary cannot store duplicate keys.

If you assign the same key again, Python replaces the old value.

Example:

product = {"name": "Book", "price": 10}
product["price"] = 12

print(product)
# {'name': 'Book', 'price': 12}

Important points:

  • Dictionaries keep one value per key
  • Reusing a key updates the value
  • Python does not warn you when a value is overwritten

This is a common source of bugs for beginners, especially when the key name is typed incorrectly or reused by mistake.

Add a key only if it is missing

Sometimes you want to add a key only if it does not already exist.

A simple beginner-friendly pattern is:

if key not in my_dict:
    my_dict[key] = value

Example:

user = {"name": "Mina"}

if "age" not in user:
    user["age"] = 30

print(user)
# {'name': 'Mina', 'age': 30}

This helps you avoid overwriting existing data.

Another example:

user = {"name": "Mina", "age": 22}

if "age" not in user:
    user["age"] = 30

print(user)
# {'name': 'Mina', 'age': 22}

Because "age" is already present, the value stays the same.

If you want to learn this check in more detail, read how to check if a key exists in a dictionary in Python.

Common beginner mistakes

Here are some common problems when adding keys to dictionaries.

Using append() on a dictionary

append() is a list method, not a dictionary method.

This is wrong:

data = {"name": "Ana"}
# data.append("age", 25)

Use bracket assignment instead:

data = {"name": "Ana"}
data["age"] = 25

print(data)
# {'name': 'Ana', 'age': 25}

Forgetting that keys must be unique

This code does not create two "age" keys:

person = {"age": 20}
person["age"] = 25

print(person)
# {'age': 25}

The old value is replaced.

Expecting update() to keep old values for the same key

update() also overwrites matching keys:

settings = {"theme": "light"}
settings.update({"theme": "dark"})

print(settings)
# {'theme': 'dark'}

Using a variable name as a string by mistake

This mistake is easy to make:

key_name = "age"
person = {"name": "Ana"}

person[key_name] = 25
print(person)
# {'name': 'Ana', 'age': 25}

That uses the value inside key_name, which is "age".

But this is different:

key_name = "age"
person = {"name": "Ana"}

person["key_name"] = 25
print(person)
# {'name': 'Ana', 'key_name': 25}

Now the actual key is the text "key_name".

If your dictionary is not changing the way you expect, these quick checks can help:

print(my_dict)
print(my_dict.keys())
print("age" in my_dict)
print(type(my_dict))

These checks help you:

  • See the current dictionary contents
  • See all keys
  • Check whether a key exists
  • Confirm that the object is really a dictionary

If you run into missing-key problems while reading values, see how to access values in a dictionary in Python and how to fix KeyError when accessing dictionary values.

FAQ

How do I add a new key to a dictionary in Python?

Use square bracket assignment:

my_dict["new_key"] = value

What if the key already exists?

Python updates the value for that key instead of creating a second copy.

How do I add many keys at once?

Use update() with another dictionary:

my_dict.update({"a": 1, "b": 2})

Can a Python dictionary have duplicate keys?

No. Each key must be unique. Reusing a key replaces the old value.

How do I add a key only if it does not exist?

Check first, then assign:

if key not in my_dict:
    my_dict[key] = value

See also