partition - filtering function in Kotlin

Authors
partition - filtering function in Kotlin

partition is a Kotlin filtering function that splits a collection into two lists: one with the elements that match the given condition, and another with the elements that do not match it.

In this blog, we will learn about the Kotlin filtering function - partition. It filters a collection by a predicate and keeps the elements that don't match it in a separate list.

I am Amit Shekhar, Founder @ Outcome School, I have taught and mentored many developers, and their efforts landed them high-paying tech jobs, helped many tech companies in solving their unique problems, and created many open-source libraries being used by top companies. I am passionate about sharing knowledge through open-source, blogs, and videos.

I teach AI and Machine Learning, and Android at Outcome School.

Join Outcome School and get a high-paying tech job:

Let's get started.

There are many useful collection functions in Kotlin. It is good to know about those and use those based on the requirement. One of those collection functions is partition.

partition filters a collection by a predicate and keeps the elements that don't match it in a separate list.

Let's learn by example.

Consider a data class User like below:

data class User(val id: Int, val name: String, val isMentor: Boolean)

And, a list of User:

val users = arrayOf(
    User(1, "Amit", true),
    User(2, "Ronaldo", false),
    User(1, "Messi", true),
    User(3, "Neymar", false))

Now, let's use the partition function on this list of users to filter the users who are a mentor and the users who are not a mentor.

val (mentors, notMentors) = users.partition { it.isMentor }

Now, let's print mentors

println(mentors)

This will print the following:

[User(id=1, name=Amit, isMentor=true),
User(id=1, name=Messi, isMentor=true)]

And, when we print notMentors

println(notMentors)

This will print the following:

[User(id=2, name=Ronaldo, isMentor=false),
User(id=3, name=Neymar, isMentor=false)]

If we go through the source code, we will find the following implementation:

public inline fun <T> Array<out T>.partition(predicate: (T) -> Boolean): Pair<List<T>, List<T>> {
    val first = ArrayList<T>()
    val second = ArrayList<T>()
    for (element in this) {
        if (predicate(element)) {
            first.add(element)
        } else {
            second.add(element)
        }
    }
    return Pair(first, second)
}

It is doing the same thing that we would have also done. It is just that Kotlin provides these useful functions out of the box so that we can use them directly.

A quick note for you

No matter which tech domain you work in, get familiar with these topics:

  • LLM
  • RAG
  • MCP
  • Agent
  • Fine-tuning
  • Quantization

We put it all together in one video:

AI Engineering Explained: LLM, RAG, MCP, Agent, Fine-Tuning, and Quantization

No need to stop reading - bookmark it and watch later when you get time. Future you will thank you.

Now, let's get back to the topic.

Note:

  • It takes a predicate.
  • It splits the original array into pair of lists and returns Pair<List<T>, List<T>>.
  • The first list contains elements for which the predicate yields true.
  • The second list contains elements for which the predicate yields false.

To master Kotlin filtering functions like partition, higher-order functions, inline functions, and extension functions hands-on, we have a complete Android Program by Outcome School.

Frequently Asked Questions

What does partition return in Kotlin?

partition returns a Pair of two lists. The first list contains the elements for which the predicate yields true. The second list contains the elements for which the predicate yields false.

How do we get both lists from partition in one line?

We can destructure the returned pair directly. For example, val (mentors, notMentors) = users.partition { it.isMentor } gives us the mentors in the first variable and the users who are not mentors in the second variable.

Does partition do anything special internally?

No. It creates two lists, loops over the elements, puts each element into the first list if the predicate returns true or into the second list otherwise, and then returns both as a pair. Kotlin just provides this function out of the box so we can use it directly.

That's it for now.

Thanks

Amit Shekhar
Founder @ Outcome School

You can connect with me on:

Follow Outcome School on:

Read all of our high-quality blogs here.

Subscribe to our newsletter to get our latest AI and Machine Learning blogs straight to your inbox.