unzip

public fun <T, R> Iterable<Pair<T, R>>.unzip(): Pair<List<T>, List<R>> {
    val expectedSize = collectionSizeOrDefault(10)
    val listT = ArrayList<T>(expectedSize)
    val listR = ArrayList<R>(expectedSize)
    for (pair in this) {
        listT.add(pair.first)
        listR.add(pair.second)
    }
    return listT to listR
}


val list = listOf(Pair(1, 5), Pair(2, 6), Pair(3, 7))
println(list.unzip())  // ([1, 2, 3], [5, 6, 7])

Last updated