Start learning free

LESSON 127 OF 677 · DICTIONARIES

Updating Dictionary Values in Kotlin

Lesson

In Kotlin, you update a value in a MutableMap by assigning to an existing key with the index operator — it overwrites the old value:

val scores = mutableMapOf<String, Int>()
scores["Alice"] = 95
scores["Bob"] = 87

scores["Alice"] = 99   // update: Alice is now 99
scores["Charlie"] = 72 // add new entry

The index operator [] is used for both inserting and updating. You can also use replace() to only update existing keys:

scores.replace("Alice", 99)  // only updates if key exists

Assignment
Follow the Coding Tutorial and update Alice's score to 99!


Hint
Use scores["Alice"] = 99.

STEP 1 OF 1
    Ctrl + Enter to check