List — zip() / unzip()
In Kotlin, zip() returns a list of pairs by combining two lists element by element. unzip() splits a list of pairs back into two separate lists. Use these functions for parallel processing of lists or when merging multiple data sequences.
Syntax
val names = listOf("Alice", "Bob", "Carol")
val scores = listOf(85, 72, 91)
// zip — pairs corresponding elements into Pair objects.
val zipped = names.zip(scores)
// [Pair("Alice", 85), Pair("Bob", 72), Pair("Carol", 91)]
// zip with transform — applies a custom transformation.
val results = names.zip(scores) { name, score -> "$name: $score points" }
// unzip — splits a list of Pairs back into two lists.
val (n, s) = zipped.unzip() // restores names and scores.
Method List
| Method | Description |
|---|---|
| listA.zip(listB) | Combines two lists into a list of Pairs, truncated to the length of the shorter list. |
| listA.zip(listB) { a, b -> transform } | Zips two lists using a transform lambda. |
| pairList.unzip() | Splits a list of Pair<A, B> into a Pair<List<A>, List<B>>. |
| list.zipWithNext() | Returns a list of pairs of adjacent elements (useful for computing differences). |
| list.zipWithNext { a, b -> } | Transforms adjacent element pairs and returns a list. |
Sample Code
fun main() {
// Basic zip
val names = listOf("Alice", "Bob", "Carol", "Dave")
val scores = listOf(85, 72, 91, 68)
val ages = listOf(25, 30, 28) // truncated to the shorter length (3 items)
val zipped = names.zip(scores)
println("zip: $zipped")
// zip with transform
val results = names.zip(scores) { name, score ->
"$name: ${score} points (${if (score >= 80) "Pass" else "Fail"})"
}
results.forEach { println(it) }
println()
// Different lengths (truncated to the shorter list)
val nameAge = names.zip(ages)
println("Different length zip: $nameAge") // 3 items
println()
// unzip — splits Pairs back into two lists.
val (nameList, scoreList) = zipped.unzip()
println("Names: $nameList")
println("Scores: $scoreList")
println()
// zipWithNext — creates pairs of adjacent elements (for computing differences)
val temps = listOf(20.0, 22.5, 19.0, 25.0, 23.0)
println("Temperature changes:")
temps.zipWithNext { today, tomorrow ->
val diff = tomorrow - today
val dir = if (diff > 0) "↑" else if (diff < 0) "↓" else "→"
println(" ${today}°C $dir ${tomorrow}°C (${"%.1f".format(diff)})")
}
println()
// Zipping 3 lists (using zip twice)
val first = listOf(1, 2, 3)
val second = listOf("a", "b", "c")
val third = listOf(true, false, true)
val triple = first.zip(second).zip(third) { (a, b), c -> Triple(a, b, c) }
triple.forEach { println(it) }
}
Overview
zip() is useful when you want to process two lists in parallel. If the lists have different lengths, the result is truncated to the shorter one — no exception is thrown.
zipWithNext() is useful for computing differences or rates of change between consecutive elements. It replaces index-based for loops.
For flattening lists, see Lists — flatMap() / flatten(). For folding, see Lists — reduce() / fold().
If you find any errors or copyright issues, please contact us.