0Pricing
Kotlin Academy · Lesson

Grouping and Partitioning

Organize data.

Grouping and Partitioning is a free Kotlin Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Organizing Data

Sometimes you need to split a collection into categories. Kotlin offers groupBy to bucket elements by a key and partition to split into two lists by a condition.

The partition Function

partition splits a collection into a Pair of two lists: elements that match the predicate, and elements that do not.

fun main() {
    val numbers = listOf(1, 2, 3, 4, 5, 6)
    val (even, odd) = numbers.partition { it % 2 == 0 }
    println("Even: $even")
    println("Odd: $odd")
}

Destructuring the Pair

Because partition returns a Pair, you can destructure it directly into two named variables, as shown above. You can also access .first and .second.

fun main() {
    val result = listOf(10, -5, 3, -8).partition { it >= 0 }
    println(result.first)
    println(result.second)
}

The groupBy Function

groupBy creates a Map where each key is produced by the selector and the value is the list of elements that share that key.

fun main() {
    val words = listOf("apple", "banana", "avocado", "cherry")
    val byFirstLetter = words.groupBy { it.first() }
    println(byFirstLetter)
}

Grouping by a Computed Key

The key can be any computed value, such as the length of a string or a remainder.

fun main() {
    val numbers = listOf(1, 2, 3, 4, 5, 6, 7)
    val byRemainder = numbers.groupBy { it % 3 }
    println(byRemainder)
}

groupBy with a Value Transform

groupBy accepts a second lambda that transforms each value before it is placed in the group.

fun main() {
    val words = listOf("red", "rose", "blue", "berry")
    val grouped = groupExample(words)
    println(grouped)
}

fun groupExample(words: List<String>) =
    words.groupBy({ it.first() }, { it.uppercase() })

Iterating Over Groups

Since groupBy returns a Map, you can iterate over entries to process each group.

fun main() {
    val animals = listOf("cat", "cow", "dog", "duck")
    val groups = animals.groupBy { it.first() }
    for ((letter, list) in groups) {
        println("$letter -> $list")
    }
}

Counting With groupingBy

For counting per key, groupingBy followed by eachCount is efficient and reads clearly.

fun main() {
    val letters = listOf("a", "b", "a", "c", "b", "a")
    val counts = letters.groupingBy { it }.eachCount()
    println(counts)
}

partition vs filter

When you only need matching elements, use filter. When you need BOTH the matching and non-matching elements, partition does it in a single pass and returns both lists.

fun main() {
    val ages = listOf(15, 22, 17, 30, 12)
    val (adults, minors) = ages.partition { it >= 18 }
    println("Adults: $adults, Minors: $minors")
}

Choosing groupBy vs partition

Use partition for exactly two buckets defined by a yes/no condition. Use groupBy when there can be many buckets keyed by some property.

Practical Grouping

Grouping and partitioning replace verbose manual loops with maps and lists. They express intent clearly and reduce bookkeeping errors.

Quick Check

What does partition return?

Recap

You learned partition to split into two lists by a condition and groupBy to bucket into a map by a key, plus groupingBy().eachCount() for counts. Next you will reduce collections to single values with aggregation.

Frequently asked questions

Is the “Grouping and Partitioning” lesson free?

Yes — the full text of “Grouping and Partitioning” is free to read here on the web, and the Kotlin Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Kotlin Academy course, upgrade to CoddyKit PRO.

What will I learn in “Grouping and Partitioning”?

Organize data. You practise Kotlin Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Kotlin Academy?

No prior experience is required. Kotlin Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Grouping and Partitioning” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Kotlin Academy lesson?

Yes. Every Kotlin Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Filtering and Mapping
  2. Grouping and Partitioning
  3. Aggregation
  4. Association and Flattening
← Back to Kotlin Academy