Menu
Coddy logo textTech

Change Values

Lesson 5 of 14 in Coddy's Dictionary in Python course.

To change the value of a specific item refer to its key name,

my_dict = {"name": "Alice", "age": 30, "city": "New York"}
my_dict["age"] = 31  # Update the age

It's also possible to use the update() method,

my_dict.update({"age": 31})

Both of the operations does the same

challenge icon

Challenge

Easy

You are given a stock dictionary, your task:

  1. Update the stock of apples to 12.
  2. Reduce the stock of cherries by 2.
  3. Increase the stock of dates by 3.
  4. Decrease the stock of bananas by 5.

Try it yourself

# The stock dictionary
stock = {
    "apple": 10,
    "banana": 15,
    "cherry": 5,
    "date": 8
}

# Your code here:

# Don't change below this line
print("Updated Stock:", stock)

All lessons in Dictionary in Python