Open In App

Kotlin partition() Method with Examples

Last Updated : 15 Jun, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

While coding in Kotlin, there are times when we want to split a list into two separate collections based on a condition for example, separating even and odd numbers. Normally, we would do this using loops and if conditions, but Kotlin makes this task easier with the partition() function. In this article, we will learn how to use the partition() method in Kotlin to split a collection into two parts based on a condition, without writing extra loops or manual filtering logic.

partition() function

Kotlin provides the partition() method for collections such as lists, sets, and arrays. According to the official documentation, the partition() function splits the original collection into a pair of lists:

  1. The first list contains all elements for which the provided condition (predicate) returns true.
  2. The second list contains all elements for which the predicate returns false.

It returns a Pair of lists where one satisfying the condition and one not.

Use cases of partition() function

We use partition() in the following cases:

  • We want to divide a collection into two parts based on a condition.
  • We don’t want to manually write for or while loops.
  • We want clean, readable Kotlin code.

Split a List of Numbers into Even and Odd

Let’s see how we can split a list of numbers into even and odd numbers using partition()

Example:

Kotlin
fun main(){
    val numbers = listOf(1, 2, 3, 4, 5, 6)

    val (even, odd) = numbers.partition { it % 2 == 0 }
    
    println("Even numbers: $even")
    println("Odd numbers: $odd")
}

Output:

Even numbers: [2, 4, 6]
Odd numbers: [1, 3, 5]

Explanation:

In the above example we pass a condition (it % 2 == 0) to partition(). It splits the list into two parts:

  1. First list (even numbers): elements satisfying the condition.
  2. Second list (odd numbers): elements not satisfying the condition.


Using partition() with a Set

The partition() function also works with other collections, like sets.

Example:

Kotlin
fun main() {
    val numberSet = setOf(1, 2, 3, 4, 5, 6)
    
    val (evenSet, oddSet) = numberSet.partition { it % 2 == 0 }
    
    println("Even numbers in set: $evenSet")
    println("Odd numbers in set: $oddSet")
}

Output:

Even numbers in set: [2, 4, 6]
Odd numbers in set: [1, 3, 5]

Even though the original collection was a set, partition() returns two lists, not sets.


Using partition() with an Array

partition() works perfectly with arrays too.

Kotlin
fun main() {
    val numberArray = arrayOf(1, 2, 3, 4, 5, 6)

    val (evenArray, oddArray) = numberArray.partition { it % 2 == 0 }
    
    println("Even numbers in array: $evenArray")
    println("Odd numbers in array: $oddArray")
}

Output:

Even numbers in array: [2, 4, 6]
Odd numbers in array: [1, 3, 5]

Regardless of the collection type (list, set, or array), the result of partition() is always a Pair of two lists.


Internal workings of partition() function

Internally, the partition() method loops over each element in the collection and checks the condition. If the element satisfies the condition, it goes into the first list, otherwise, into the second list. Using partition() saves us from writing manual code in the following way.

Example:

Kotlin
fun main() {
    val evens = mutableListOf<Int>()
    val odds = mutableListOf<Int>()
    
    for (number in numbers) {
        if (number % 2 == 0) {
            evens.add(number)
        } else {
            odds.add(number)
        }
    }
}

Instead, the entire operation is reduced to the following which is much shorter, cleaner, and easier to read.

val (evens, odds) = numbers.partition { it % 2 == 0 }

Next Article

Similar Reads