Python – Change Dictionary Items

1. What Does โ€œChange Dictionary Itemsโ€ Mean?

Changing dictionary items means updating the value of an existing key.

In simple words:
๐Ÿ‘‰ We change value, not the key.

2. Change Value Using Key Name

The easiest way is using the key.

Example

student = {
"name": "Amit",
"age": 20,
"city": "Delhi"
}

student["age"] = 21
print(student)

Output:

{'name': 'Amit', 'age': 21, 'city': 'Delhi'}

3. Change Value Using update()

update() is another easy way to change values.

Example

student.update({"city": "Kolkata"})
print(student)

4. Change Multiple Values at Once

Example

student.update({
"age": 22,
"city": "Mumbai"
})

print(student)

5. Change Value Only If Key Exists

Example

if "marks" in student:
student["marks"] = 90

6. Change Value in Nested Dictionary

Example

data = {
"student": {
"name": "Amit",
"age": 20
}
}

data["student"]["age"] = 21
print(data)

7. Change Values Using Loop

Example

prices = {
"pen": 10,
"book": 50
}

for item in prices:
prices[item] = prices[item] + 5

print(prices)

8. Common Beginner Mistakes

โŒ Trying to Change Key Name

student["Name"] = student.pop("name")

๐Ÿ‘‰ This removes old key and adds new one.

โŒ Accessing Missing Key

student["phone"] = "123456"

โœ” This adds a new key, not changes existing one.

9. Simple Practice Examples

Example 1: Update Product Price

product = {
"name": "Mobile",
"price": 15000
}

product["price"] = 14000
print(product)

Example 2: Update City

person = {"name": "Ravi", "city": "Delhi"}
person.update({"city": "Pune"})
print(person)

Example 3: Increase Marks

marks = {"math": 60, "science": 70}

for subject in marks:
marks[subject] += 5

print(marks)

10. Summary (Change Dictionary Items)

โœ” Use key to change value
โœ” update() is easy and clean
โœ” Can update many values at once
โœ” Values can be changed anytime
โœ” Keys should be unique

๐Ÿ“˜ Perfect for Beginner eBook

This chapter is ideal for:

  • Python beginner books

  • School & college students

  • Self-learners

If you want next, I can write:

  • Add Dictionary Items

  • Remove Dictionary Items

  • Dictionary Methods

  • Dictionary Exercises

Just tell me ๐Ÿ˜Š

You may also like...