The 'partition' function is a convenient way to split a collection into two lists based on a predicate. It returns a Pair of lists: the first list contains elements that match the predicate, while the second list contains the remaining elements. This is useful when you want to categorize data into two groups without needing to filter and then create separate lists manually.
val numbers = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
// Partition the numbers into even and odd
val (evenNumbers, oddNumbers) = numbers.partition { it % 2 == 0 }
println("Even Numbers: $evenNumbers") // Output: Even Numbers: [2, 4, 6, 8, 10]
println("Odd Numbers: $oddNumbers") // Output: Odd Numbers: [1, 3, 5, 7, 9]