Kotlin partition() Method with Examples
Last Updated :
15 Jun, 2025
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:
- The first list contains all elements for which the provided condition (predicate) returns true.
- 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:
- First list (even numbers): elements satisfying the condition.
- 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 }
Similar Reads
Kotlin Aggregate operations When we work with collections like Lists or Arrays in Kotlin, sometimes we want to perform an operation on the entire collection rather than on individual elements. These are called aggregate operations because they "aggregate" (or combine) the collection data to give us a single result, such as a t
5 min read
Kotlin | Retrieve Collection Parts The slice function allows you to extract a list of elements from a collection by specifying the indices of the elements you want to retrieve. The resulting list contains only the elements at the specified indices. The subList function allows you to retrieve a sublist of a collection by specifying th
5 min read
Kotlin Tutorial This Kotlin tutorial is designed for beginners as well as professional, which covers basic and advanced concepts of Kotlin programming language. In this Kotlin tutorial, you'll learn various important Kotlin topics, including data types, control flow, functions, object-oriented programming, collecti
4 min read
Destructuring Declarations in Kotlin Kotlin provides a concise and elegant way to extract multiple values from an object into separate variables using destructuring declarations. This feature allows you to break an object into its components and work with each piece independently.Destructuring DeclarationA destructuring declaration unp
3 min read
A Complete Guide to Learn Kotlin For Android App Development Kotlin is a statically typed, cross-platform, general-purpose programming language for JVM developed by JetBrains. This is a language with type inference and fully interoperable with Java. Kotlin is concise and expressive programming as it reduces the boilerplate code. Since Google I/O 2019, Android
8 min read