🛠️ How to Change, Update, or Add Items in a Python Dictionary | TechTown.in

Dictionaries in Python are mutable, which means you can modify, add, or overwrite values anytime — even after the dictionary is created.

In this guide, we’ll explore all the ways to update and manage dictionary items in Python — using direct assignment, .update() method, and real-life examples.


📦 Example Dictionary

person = {
    "name": "Tanmay",
    "age": 22,
    "city": "Jaipur"
}

🔁 1. Changing the Value of an Existing Key

Just use the key name and assign a new value:

person["age"] = 23
print(person)
# {'name': 'Tanmay', 'age': 23, 'city': 'Jaipur'}

✅ Quick and direct.


➕ 2. Adding a New Key-Value Pair

Use the same syntax — if the key doesn’t exist, Python will add it.

person["email"] = "tanmay@example.com"
print(person)

🎯 Output:

{'name': 'Tanmay', 'age': 23, 'city': 'Jaipur', 'email': 'tanmay@example.com'}

🔄 3. Use update() Method (Add or Modify)

person.update({"age": 24, "gender": "Male"})
print(person)

📌 This method can update existing keys and add new ones at the same time.


🧠 Real-Life Example: Updating Product Info

product = {
    "id": 101,
    "name": "Laptop",
    "price": 60000
}

product["price"] = 57999  # price drop
product.update({"stock": 12})

Useful for eCommerce apps, admin panels, and APIs.


🧪 Bonus: Nested Dictionary Update

student = {
    "name": "Aditi",
    "marks": {"math": 80, "science": 85}
}

student["marks"]["math"] = 90

✅ You can even update values inside nested dictionaries!


📝 Summary – Ways to Update a Dictionary in Python

TaskMethodUse Case
Change existing valuedict[key] = new_valueQuick updates
Add new key-value pairdict[new_key] = valueExtend dictionary
Modify multiple valuesdict.update({...})Batch updates
Nested dictionary updatedict[key][subkey] = valueComplex data structures

🏁 Final Thoughts

Dictionaries in Python are extremely flexible. Whether you’re handling user profiles, updating product data, or modifying API responses — knowing how to change and add values is essential.

Use direct assignment for simple changes, and .update() when you want to add or modify multiple items at once.


📘 Learn more about Python dictionaries at TechTown.in