Using the 'zip' Function for Combining Collections

The 'zip' function in Kotlin is a powerful tool for combining two collections into a single collection of pairs. Each element from the first collection is paired with the corresponding element from the second collection, creating a new collection of pairs. This is particularly useful when you need to work with two related lists, such as a list of names and a list of ages, allowing you to easily access both values together.

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

val combined = names.zip(ages)

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