Using the 'zip' Function for Pairing Collections

The 'zip' function in Kotlin allows you to combine two collections into a single collection of pairs. Each pair consists of elements at the same index from the two collections. This is particularly useful when you need to correlate data from two different lists, such as combining a list of names with a list of ages to create a list of people.

val names = listOf("Alice", "Bob", "Charlie")
val ages = listOf(25, 30, 35)

val paired = names.zip(ages)

paired.forEach { (name, age) ->
    println("$name is $age years old")
}