Kotlin example

How to initialize a list in Kotlin

8 min read Updated Dec 2026 Runs in an isolated runtime
Quick answer

Use listOf("a", "b") for a list you only read, and mutableListOf("a", "b") for one you add to or remove from. An empty mutable list is mutableListOf<String>(), and a list of n values is List(n) { index -> value }. A plain List has no add() on purpose; call toMutableList() to get a copy you can change.

Kotlin splits lists into two types. List<T> is read-only: you can get, search and loop, but there is no add, remove or set. MutableList<T> adds those. Which one you get depends on the function that creates the list, so "how do I initialize a list" is really "which of these functions do I call". The sections below cover listOf and mutableListOf, empty lists, the compile error you get when you call add on a List, sized and repeated lists with List(n) { } and buildList, ArrayList next to mutableListOf, and converting between the two types, then a table to pick from. Each example runs on this page: hit Run, then edit the code and run it again.

1listOf and mutableListOfRecommended

listOf(…) takes the values and returns a read-only List; mutableListOf(…) does the same and returns a MutableList. Kotlin infers the element type from the values, or from the declared type when you give one: val ids: List<Long> = listOf(1, 2, 3) makes Longs. Add with add or +=, insert with add(index, value), replace with list[i] = value, and remove by value with remove or by position with removeAt. On a read-only list, + returns a new list and leaves the old one as it was.

ListOfMutableListOf.kt
fun main() {
    // Read-only: fixed contents, no add or remove
    val days = listOf("Mon", "Tue", "Wed")
    println(days)
    println("${days.size} days, first ${days.first()}, last ${days[2]}")

    // The declared type picks the element type
    val ids: List<Long> = listOf(1, 2, 3)
    println("${ids[0]::class.simpleName} ${ids.sum()}")

    // Mutable: add, insert, replace and remove
    val tasks = mutableListOf("write", "test")
    tasks.add("ship")
    tasks.add(0, "plan")
    tasks += "rest"
    tasks[1] = "draft"
    tasks.remove("test")
    tasks.removeAt(tasks.lastIndex)
    println(tasks)

    // Several at once
    tasks += listOf("review", "merge")
    tasks.removeAll { it.startsWith("m") }
    println(tasks)

    // + on a read-only list builds a new list
    val more = days + "Thu"
    println("$days -> $more")
}

Output

ids[0]::class.simpleName prints Long: the literals 1, 2, 3 took the type from the declaration. tasks goes through every change in place and ends as [plan, draft, ship]: the add(0, …) put plan first, the assignment replaced write, and the two removes took out test and rest. On the last line days is still three items long, because + made a new list. Printing a list calls its toString(), which gives the [a, b, c] form.

2An empty List or MutableList

An empty list has no values to infer the type from, so you name it: either on the function, mutableListOf<String>(), or on the variable, val names: MutableList<String> = mutableListOf(). Leave both out and the compiler stops with Cannot infer type for type parameter 'T'. Specify it explicitly. For a read-only empty list, use emptyList<String>(). Java habits don't carry over here: MutableList is an interface, and MutableList<String>() calls a function that needs a size and a lambda (section 4). For a nullable list, orEmpty() gives an empty list in place of null.

EmptyLists.kt
class Cart {
    // A property that starts empty and fills up later
    val items = mutableListOf<String>()

    fun add(item: String) {
        items += item
    }
}

fun main() {
    // Read-only empty lists: the type goes in <> or on the variable
    val none = emptyList<String>()
    val alsoNone: List<Int> = listOf()
    println("$none $alsoNone ${none.isEmpty()}")

    // Empty mutable lists, filled later
    val names = mutableListOf<String>()
    val scores: MutableList<Int> = mutableListOf()
    names.add("Ada")
    scores += 90
    scores += listOf(75, 82)
    println("$names $scores")

    val cart = Cart()
    cart.add("tea")
    cart.add("milk")
    println(cart.items)

    // A var holding a read-only list: += builds a new list each time
    var log = listOf<String>()
    log += "start"
    log += "stop"
    println(log)

    // null -> empty list
    val fromApi: List<String>? = null
    println(fromApi.orEmpty().size)
}

Output

Both empty lists print [], and scores ends as [90, 75, 82]: += takes a single value or a whole list. The Cart keeps its list in a val; the list changes but the property never points anywhere else, which is the usual shape for a class that collects things. log is the other way round, a var holding a read-only list, so each += builds a new list and reassigns it. Both work; keep a MutableList in a val so that += can only mean "add in place".

3The compile error: List has no add()

Code ported from Java often calls add on a list made with listOf. That doesn't compile: the read-only List interface has no add, remove or set, and the same goes for Map, which has no put (use mutableMapOf). The program below never runs.

ListHasNoAdd.kt
fun main() {
    val names = listOf("Ada", "Linus")
    names.add("Grace")
    println(names)
}

Output

The compiler stops with Unresolved reference 'add' on receiver of type 'List<String>'. (for a map: Unresolved reference 'put' on receiver of type 'Map<String, Int>'.). There are two fixes. If the list should grow, create it with mutableListOf on line 2. If it should stay fixed and you want a longer version, write val more = names + "Grace" and print more. Either fix prints [Ada, Linus, Grace].

4List(n) { }, repeated values and buildList

List(n) { index -> value } creates a list of n items by calling the lambda once per index, and MutableList(n) { … } does the same for a mutable one. With a lambda that ignores the index, List(3) { "-" }, you get the same value repeated. Ranges turn into lists with toList(). When the contents depend on conditions or loops, buildList { … } gives you a MutableList inside the block and returns a read-only List, so you don't need a temporary mutable variable.

SizedAndRepeated.kt
fun main() {
    // List(size) { index -> value }
    val squares = List(5) { it * it }
    println(squares)

    // The same value n times
    println(List(3) { "-" })
    val counts = MutableList(4) { 0 }
    counts[2] += 7
    println(counts)

    // A whole list repeated
    println(List(3) { listOf("a", "b") }.flatten())

    // Ranges
    println((1..5).toList())
    println((10 downTo 0 step 5).toList())

    // Each row gets its own list
    val grid = List(3) { mutableListOf<Int>() }
    grid[0].add(1)
    println(grid)

    // nCopies repeats one object, so all rows are the same list
    val shared = java.util.Collections.nCopies(3, mutableListOf<Int>())
    shared[0].add(1)
    println(shared)

    // buildList: add in steps, get a read-only List back
    val verbose = true
    val args = buildList {
        add("--port=8080")
        if (verbose) add("--verbose")
        for (n in 1..2) add("file$n.txt")
    }
    println(args)
}

Output

List(5) { it * it } gives [0, 1, 4, 9, 16], and MutableList(4) { 0 } gives four zeros you can then change. The two grid lines are the trap with repeated mutable values: List(3) calls the lambda three times and makes three lists, so only the first row gets the 1. Java's Collections.nCopies stores one list three times, so adding to "row 0" shows up in every row as [[1], [1], [1]]. Use the lambda form whenever the repeated value is mutable.

5ArrayList vs mutableListOf

ArrayList<String>() works in Kotlin (there is no new), and so does arrayListOf(…). On the JVM, mutableListOf also creates a java.util.ArrayList, so the three behave the same at run time. The difference is the type you get: mutableListOf returns the MutableList interface, while the other two return the class ArrayList. Prefer mutableListOf and declare parameters and properties as MutableList or List; reach for ArrayList when you want its extra methods, such as an initial capacity or trimToSize(), or when a Java API asks for one.

ArrayListVsMutableList.kt
fun fill(target: MutableList<String>, vararg items: String) {
    target += items
}

fun main() {
    val a = mutableListOf<String>()     // type: MutableList<String>
    val b = arrayListOf<String>()       // type: ArrayList<String>
    val c = ArrayList<String>()         // type: ArrayList<String>

    // All three are java.util.ArrayList at run time
    for (list in listOf(a, b, c)) {
        fill(list, "x", "y")
        println("${list::class.qualifiedName} $list")
    }
    println(a == b && b == c)

    // ArrayList-only extras: an initial capacity and trimToSize()
    val big = ArrayList<Int>(10_000)
    for (i in 1..3) big.add(i)
    big.trimToSize()
    println(big)

    // Java's List.of is truly immutable: add throws
    val fixed = java.util.List.of(1, 2)
    try {
        fixed.add(3)
    } catch (e: UnsupportedOperationException) {
        println("List.of: ${e::class.simpleName}")
    }
}

Output

All three lists print java.util.ArrayList [x, y], compare equal, and pass to fill, which asks only for a MutableList. That is the reason to type parameters with the interface: callers can pass any of them. The capacity of 10_000 only reserves room; the list still holds three items and prints [1, 2, 3]. Java's List.of returns a list whose add throws UnsupportedOperationException, even though Kotlin sees it as a MutableList and compiles the call.

6List to MutableList, and read-only vs immutable

toMutableList() copies any list (or set, array or range) into a new MutableList; toList() copies the other way, into a read-only snapshot. Both are copies, so later changes to one side don't reach the other. Read-only is not the same as immutable, though: a List variable only promises that you can't change the list through it. If it points at a MutableList, whoever holds that can still change it, and you see the changes. Casting with as MutableList is not a conversion either.

ToMutableList.kt
fun main() {
    val original = listOf(3, 1, 2)

    // toMutableList() copies into a new MutableList
    val copy = original.toMutableList()
    copy += 4
    copy.sort()
    println("$original $copy")

    // A read-only view of a mutable list still changes
    val backing = mutableListOf("a", "b")
    val view: List<String> = backing
    backing += "c"
    println(view)

    // toList() takes a snapshot that stays put
    val snapshot = backing.toList()
    backing.clear()
    println("view=$view snapshot=$snapshot")

    // A cast is not a copy: it changes the original...
    val nums = listOf(1, 2, 3)
    val forced = nums as MutableList<Int>
    forced[0] = 9
    println(nums)
    // ...and adding to a listOf list throws
    forced.add(4)
    println("never printed")
}

Output

original stays [3, 1, 2] while the copy is sorted and grows to [1, 2, 3, 4]. view is typed List, yet it shows c after backing += "c", and it is empty after backing.clear(); the toList() snapshot keeps [a, b, c]. The cast compiles and lets forced[0] = 9 change nums itself, then add stops the program with Exception in thread "main" java.lang.UnsupportedOperationException at Main.kt:27: a listOf list of several items is a fixed-size Java list that allows set but not add. Call toMutableList() instead of casting.

7Which should you use?

MethodType you getCan change?Best for
listOf(a, b, c)List<T>NoFixed values you only read
mutableListOf(a, b)MutableList<T>YesA list you add to and remove from
mutableListOf<T>()MutableList<T>YesStarting empty and filling later
emptyList<T>()List<T>NoDefault values and early returns
List(n) { i -> … }List<T>No (MutableList(n): yes)n items, computed or repeated
buildList { … }List<T>Only inside the blockContents built with ifs and loops
list.toMutableList()MutableList<T>Yes, a copyChanging a list you were given
ArrayList<T>() / arrayListOf()ArrayList<T>YesInitial capacity, Java APIs that need ArrayList

Frequently asked questions

How do I create an empty MutableList in Kotlin?

Write val items = mutableListOf<String>(), or put the type on the variable: val items: MutableList<String> = mutableListOf(). Without a type anywhere the compiler stops with Cannot infer type for type parameter 'T'. Specify it explicitly. MutableList<String>() is not a constructor: MutableList is an interface, and the function of that name needs a size and an init lambda. For a read-only empty list, use emptyList<String>().

Why does Kotlin's List have no add or remove method?

List is Kotlin's read-only list interface: it has get, size, contains and iteration, but nothing that changes the list. MutableList extends it with add, remove, set and clear. Create the list with mutableListOf when it needs to change. Maps work the same way: Map has no put, MutableMap (from mutableMapOf) does.

What is the difference between ArrayList<String>() and mutableListOf<String>()?

At run time on the JVM, nothing: both create a java.util.ArrayList. The difference is the static type. mutableListOf returns MutableList<String>, the interface, and ArrayList<String>() returns the class, which adds methods like ensureCapacity and trimToSize. Prefer mutableListOf and interface types; use ArrayList when you need a capacity or a Java API asks for one.

How do I add an item to a list in Kotlin?

On a MutableList, call list.add(item) or write list += item; list.add(0, item) inserts at a position and list += otherList or addAll adds several. A read-only List can't be changed, but val longer = list + item returns a new list with the item at the end.

How do I create a list with the same element repeated n times?

Use List(n) { value }, for example List(3) { "-" } gives [-, -, -], or MutableList(n) { 0 } for a mutable list of zeros. The lambda runs once per item, so List(3) { mutableListOf<Int>() } makes three separate lists. Collections.nCopies(n, value) stores one object n times, which is fine for strings and numbers but shares a mutable value across every slot.

How do I convert a List to a MutableList in Kotlin?

Call list.toMutableList(). It returns a new MutableList with the same items; changes to the copy don't touch the original. toList() goes the other way and takes a read-only snapshot. Don't cast with list as MutableList<T>: that is not a copy, and a list from listOf throws UnsupportedOperationException when you add to it.

Is a Kotlin List immutable?

Not necessarily. List is read-only: the interface has no methods to change it, but the object behind it may be a MutableList that other code still changes, and a List view shows those changes. For a list nobody can change, keep it private or hand out a copy with toList(). Truly immutable and persistent lists live in the separate kotlinx.collections.immutable library, which a project has to add as a dependency.

Run it yourself

Open any of these in the full Kotlin editor to tweak, run and share.

Kotlin playground