Kotlin How to Convert Map to List with Examples
In Kotlin, you can convert a map to a list by using
map.toList() which returns a list of pairs, or use map.keys.toList() or map.values.toList() to get lists of keys or values respectively.Examples
Input{1=10, 2=20}
Output[(1, 10), (2, 20)]
Input{"a"=100, "b"=200}
Output[("a", 100), ("b", 200)]
Input{}
Output[]
How to Think About It
To convert a map to a list in Kotlin, think about what you want: a list of key-value pairs, just keys, or just values. The map itself can be turned into a list of pairs using
toList(). If you want only keys or values, convert those collections to lists separately.Algorithm
1
Get the input map.2
Decide if you want a list of pairs, keys, or values.3
If pairs, call <code>toList()</code> on the map.4
If keys, call <code>keys.toList()</code>.5
If values, call <code>values.toList()</code>.6
Return the resulting list.Code
kotlin
fun main() {
val map = mapOf(1 to 10, 2 to 20)
val listPairs = map.toList()
val listKeys = map.keys.toList()
val listValues = map.values.toList()
println(listPairs) // [(1, 10), (2, 20)]
println(listKeys) // [1, 2]
println(listValues) // [10, 20]
}Output
[(1, 10), (2, 20)]
[1, 2]
[10, 20]
Dry Run
Let's trace converting map {1=10, 2=20} to a list of pairs.
1
Start with map
map = {1=10, 2=20}
2
Convert map to list of pairs
listPairs = map.toList() -> [(1, 10), (2, 20)]
3
Print the list
Output: [(1, 10), (2, 20)]
| Step | Map | List of Pairs |
|---|---|---|
| 1 | {1=10, 2=20} | N/A |
| 2 | {1=10, 2=20} | [(1, 10), (2, 20)] |
Why This Works
Step 1: Map to List of Pairs
Using toList() on a map converts each key-value pair into a Pair object inside a list.
Step 2: Keys or Values to List
You can separately convert keys or values collections to lists using keys.toList() or values.toList().
Step 3: Resulting List
The result is a standard Kotlin list you can use like any other list.
Alternative Approaches
Using map.entries.map
kotlin
fun main() {
val map = mapOf(1 to 10, 2 to 20)
val list = map.entries.map { it.toPair() }
println(list) // [(1, 10), (2, 20)]
}This creates a list by transforming each map entry explicitly; useful if you want to customize the pair.
Using flatMap for custom list
kotlin
fun main() {
val map = mapOf(1 to 10, 2 to 20)
val list = map.flatMap { listOf(it.key, it.value) }
println(list) // [1, 10, 2, 20]
}This flattens keys and values into a single list; useful if you want a combined list of all elements.
Complexity: O(n) time, O(n) space
Time Complexity
Converting a map to a list requires visiting each element once, so it takes linear time relative to the map size.
Space Complexity
A new list is created to hold all pairs or keys/values, so space grows linearly with the map size.
Which Approach is Fastest?
Using toList() is the simplest and fastest for direct conversion; alternatives add overhead for transformations.
| Approach | Time | Space | Best For |
|---|---|---|---|
| map.toList() | O(n) | O(n) | Simple direct conversion to list of pairs |
| map.entries.map { it.toPair() } | O(n) | O(n) | Custom transformation of entries |
| map.flatMap { listOf(it.key, it.value) } | O(n) | O(n) | Flatten keys and values into one list |
Use
map.toList() to quickly get a list of key-value pairs from a map.Trying to cast a map directly to a list without using
toList() causes errors.