Kotlin List zip()
Syntax & Examples
Syntax of List.zip()
There are 4 variations for the syntax of List.zip() extension function. They are:
infix fun <T, R> Iterable<T>.zip( other: Array<out R> ): List<Pair<T, R>>This extension function returns a list of pairs built from the elements of this collection and the other array with the same index. The returned list has length of the shortest collection.
fun <T, R, V> Iterable<T>.zip( other: Array<out R>, transform: (a: T, b: R) -> V ): List<V>This extension function returns a list of values built from the elements of this collection and the other array with the same index using the provided transform function applied to each pair of elements. The returned list has length of the shortest collection.
infix fun <T, R> Iterable<T>.zip( other: Iterable<R> ): List<Pair<T, R>>This extension function returns a list of pairs built from the elements of this collection and other collection with the same index. The returned list has length of the shortest collection.
fun <T, R, V> Iterable<T>.zip( other: Iterable<R>, transform: (a: T, b: R) -> V ): List<V>This extension function returns a list of values built from the elements of this collection and the other collection with the same index using the provided transform function applied to each pair of elements. The returned list has length of the shortest collection.
✐ Examples
1 Example
In this example,
- We create a list named
list1containing integers 1, 2, and 3. - We create an array named
array2containing integers 4, 5, and 6. - We use the
zipfunction to combine elements oflist1andarray2into pairs. - Since both collections have the same length, the resulting list contains pairs of corresponding elements.
- Finally, we print the
zippedlist to standard output.
Kotlin Program
fun main(args: Array<String>) {
val list1 = listOf(1, 2, 3);
val array2 = arrayOf(4, 5, 6);
val zipped = list1.zip(array2);
println(zipped);
}Output
[(1, 4), (2, 5), (3, 6)]
2 Example
In this example,
- We create a list named
list1containing characters 'a', 'b', and 'c'. - We create an array named
array2containing characters 'x', 'y', and 'z'. - We use the
zipfunction to combine elements oflist1andarray2into strings by concatenating corresponding elements. - Finally, we print the
zippedlist to standard output.
Kotlin Program
fun main(args: Array<String>) {
val list1 = listOf('a', 'b', 'c');
val array2 = arrayOf('x', 'y', 'z');
val zipped = list1.zip(array2) { a, b -> "$a$b" };
println(zipped);
}Output
[ax, by, cz]
3 Example
In this example,
- We create a list named
list1containing strings "apple", "banana", and "cherry". - We create another list named
list2containing integers 1 and 2. - We use the
zipfunction to combine elements oflist1andlist2into pairs. - Since
list2is shorter thanlist1, only the first two elements oflist1are paired with the corresponding elements oflist2. - Finally, we print the
zippedlist to standard output.
Kotlin Program
fun main(args: Array<String>) {
val list1 = listOf("apple", "banana", "cherry");
val list2 = listOf(1, 2);
val zipped = list1.zip(list2);
println(zipped);
}Output
[(apple, 1), (banana, 2)]
Summary
In this Kotlin tutorial, we learned about zip() extension function of List: the syntax and few working examples with output and detailed explanation for each example.