Kotlin Mutable Lists

A mutable list is a list you can change after creation — add items, remove items, and update existing items. Use mutableListOf() to create one. Regular listOf() creates a fixed, read-only list.

Creating a Mutable List

val cart = mutableListOf("Apple", "Milk", "Bread")
println(cart)   // [Apple, Milk, Bread]

Adding Items

val tasks = mutableListOf("Wake up", "Exercise")

tasks.add("Breakfast")              // add to end
tasks.add(0, "Check phone")        // add at index 0
tasks.addAll(listOf("Work", "Lunch")) // add multiple

println(tasks)
// [Check phone, Wake up, Exercise, Breakfast, Work, Lunch]

Removing Items

val items = mutableListOf("pen", "pencil", "eraser", "ruler", "pencil")

items.remove("pencil")       // removes FIRST occurrence of "pencil"
println(items)               // [pen, eraser, ruler, pencil]

items.removeAt(2)            // removes item at index 2
println(items)               // [pen, eraser, pencil]

items.removeAll { it.length > 5 }  // removes items where condition is true
println(items)               // [pen]

Updating Items

val scores = mutableListOf(80, 70, 90, 60)

scores[1] = 85   // update index 1 from 70 to 85
println(scores)  // [80, 85, 90, 60]

scores.set(3, 75)  // same as scores[3] = 75
println(scores)    // [80, 85, 90, 75]

Clearing and Replacing

val list = mutableListOf(1, 2, 3, 4, 5)

list.clear()
println(list)    // []
println(list.isEmpty())  // true

list.addAll(listOf(10, 20, 30))
println(list)    // [10, 20, 30]

Sorting a Mutable List

val numbers = mutableListOf(5, 2, 8, 1, 9, 3)

numbers.sort()         // sorts in place ascending
println(numbers)       // [1, 2, 3, 5, 8, 9]

numbers.sortDescending()
println(numbers)       // [9, 8, 5, 3, 2, 1]

numbers.shuffle()      // random order
println(numbers)       // random each time

numbers.reverse()      // reverses in place
println(numbers)

Mutable vs Immutable


                listOf()          mutableListOf()
─────────────────────────────────────────────────
add()           ✗ not allowed    ✓ allowed
remove()        ✗ not allowed    ✓ allowed
update [i]=     ✗ not allowed    ✓ allowed
read [i]        ✓ allowed        ✓ allowed
size, contains  ✓ allowed        ✓ allowed

Converting Between Types

val fixed = listOf(1, 2, 3)
val mutable = fixed.toMutableList()   // copy as mutable list
mutable.add(4)
println(mutable)   // [1, 2, 3, 4]
println(fixed)     // [1, 2, 3]  (original unchanged)

Practical Example: Shopping Cart Manager

fun main() {
    val cart = mutableListOf()

    fun addItem(item: String) {
        cart.add(item)
        println("Added: $item → Cart: $cart")
    }

    fun removeItem(item: String) {
        if (cart.remove(item)) {
            println("Removed: $item → Cart: $cart")
        } else {
            println("$item not in cart")
        }
    }

    addItem("Laptop")
    addItem("Mouse")
    addItem("Keyboard")
    addItem("Mousepad")
    removeItem("Mouse")
    removeItem("Monitor")
    println("Final cart (${cart.size} items): $cart")
}

Output:

Added: Laptop → Cart: [Laptop]
Added: Mouse → Cart: [Laptop, Mouse]
Added: Keyboard → Cart: [Laptop, Mouse, Keyboard]
Added: Mousepad → Cart: [Laptop, Mouse, Keyboard, Mousepad]
Removed: Mouse → Cart: [Laptop, Keyboard, Mousepad]
Monitor not in cart
Final cart (3 items): [Laptop, Keyboard, Mousepad]

Leave a Comment

Your email address will not be published. Required fields are marked *