Using the 'zip' Function for Pairing Collections

The 'zip' function in Kotlin allows you to combine two collections into a single collection of pairs. This is especially useful when you need to process two related lists together, such as combining a list of names with a corresponding list of ages. The resulting collection will contain pairs of elements from both lists, maintaining their respective positions.

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

val paired = names.zip(ages)

println(paired) // Output: [(Alice, 25), (Bob, 30), (Charlie, 35)]