Bird
Raised Fist0
Kotlinprogramming~10 mins

Partition for splitting in Kotlin - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Partition for splitting
Start with a list
↓
Apply partition(predicate)
↓
Check each element with predicate
↓
Add to first list
↓
Add to second list
↓
Return Pair of two lists
Partition splits a list into two lists based on a condition, returning a pair of lists.
Execution Sample
Kotlin
val numbers = listOf(1, 2, 3, 4, 5)
val (even, odd) = numbers.partition { it % 2 == 0 }
println("Even: $even")
println("Odd: $odd")
Splits numbers into even and odd lists using partition.
Execution Table
StepElementPredicate (it % 2 == 0)Add to Even List?Add to Odd List?
11falseNoYes
22trueYesNo
33falseNoYes
44trueYesNo
55falseNoYes
End--Partition completePartition complete
💡 All elements checked, partition returns Pair(evenList, oddList)
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5Final
even[][][2][2][2, 4][2, 4][2, 4]
odd[][1][1][1, 3][1, 3][1, 3, 5][1, 3, 5]
Key Moments - 3 Insights
Why does the partition return a Pair of lists instead of two separate lists?
Partition returns a Pair to keep the two resulting lists together as one object, making it easy to assign both at once, as shown in the execution_table where both even and odd lists are built simultaneously.
What happens if the predicate is true for all elements?
If the predicate is true for all elements, all go into the first list and the second list is empty, as the execution_table shows how elements are added based on the predicate result.
Can the original list be modified by partition?
No, partition does not change the original list; it creates two new lists, as seen in variable_tracker where only even and odd lists change, original list remains unchanged.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of the even list after step 4?
A[2]
B[1, 3]
C[2, 4]
D[1, 2, 3, 4]
💡 Hint
Check the 'Add to Even List?' column and variable_tracker row for 'even' after step 4.
At which step does the element 3 get added to the odd list?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the execution_table row where Element is 3 and see where 'Add to Odd List?' is Yes.
If the predicate was changed to 'it > 3', what would be the first element in the first list after step 3?
ANo elements yet
B4
C3
D1
💡 Hint
Check which elements satisfy 'it > 3' in the first three elements in the execution_table.
Concept Snapshot
partition(predicate): Pair<List<T>, List<T>>
Splits a list into two lists based on predicate.
First list: elements where predicate is true.
Second list: elements where predicate is false.
Returns a Pair of these two lists.
Original list remains unchanged.
Full Transcript
Partition in Kotlin takes a list and splits it into two lists based on a condition called a predicate. Each element is checked: if it meets the condition, it goes to the first list; if not, it goes to the second list. The function returns both lists together as a Pair. The original list does not change. For example, splitting numbers into even and odd lists uses partition with a condition checking if a number is even. Step by step, each number is tested and added to the correct list. This helps organize data simply and clearly.

Practice

(1/5)
1. What does the partition function do in Kotlin when applied to a list?
easy
A. Converts the list into a map with indices as keys.
B. Sorts the list in ascending order.
C. Removes duplicate elements from the list.
D. Splits the list into two lists based on a condition, returning a Pair of lists.

Solution

  1. Step 1: Understand the purpose of partition

    The partition function divides a list into two parts based on a condition.
  2. Step 2: Recognize the return type

    It returns a Pair where the first list contains elements matching the condition, and the second list contains the rest.
  3. Final Answer:

    Splits the list into two lists based on a condition, returning a Pair of lists. -> Option D
  4. Quick Check:

    partition = split by condition [OK]
Hint: Remember: partition always returns two lists in a Pair [OK]
Common Mistakes:
  • Thinking partition sorts the list
  • Confusing partition with filtering
  • Assuming partition removes duplicates
2. Which of the following is the correct syntax to use partition on a list numbers to split even and odd numbers?
easy
A. val (evens, odds) = numbers.partition { it % 2 == 0 }
B. val (evens, odds) = numbers.partition(it % 2 == 0)
C. val evens = numbers.partition { it % 2 == 0 }
D. val evens, odds = numbers.partition { it % 2 == 0 }

Solution

  1. Step 1: Check the lambda syntax inside partition

    The condition must be inside curly braces with it representing each element.
  2. Step 2: Verify destructuring declaration

    Partition returns a Pair, so use val (first, second) = ... to get both lists.
  3. Final Answer:

    val (evens, odds) = numbers.partition { it % 2 == 0 } -> Option A
  4. Quick Check:

    Correct lambda and destructuring = val (evens, odds) = numbers.partition { it % 2 == 0 } [OK]
Hint: Use val (a, b) = list.partition { condition } [OK]
Common Mistakes:
  • Missing curly braces for lambda
  • Not using destructuring for Pair
  • Using parentheses instead of braces for lambda
3. What is the output of this Kotlin code?
val words = listOf("apple", "banana", "avocado", "berry")
val (aWords, otherWords) = words.partition { it.startsWith('a') }
println(aWords)
println(otherWords)
medium
A. ["apple", "avocado"] ["banana", "berry"]
B. ["banana", "berry"] ["apple", "avocado"]
C. ["apple"] ["banana", "avocado", "berry"]
D. ["avocado"] ["apple", "banana", "berry"]

Solution

  1. Step 1: Apply the condition to each word

    Check which words start with 'a': "apple" and "avocado" match.
  2. Step 2: Partition splits into two lists

    First list has words starting with 'a', second list has the rest: "banana", "berry".
  3. Final Answer:

    ["apple", "avocado"] ["banana", "berry"] -> Option A
  4. Quick Check:

    partition by startsWith('a') = ["apple", "avocado"] ["banana", "berry"] [OK]
Hint: Check condition on each element carefully [OK]
Common Mistakes:
  • Mixing order of lists in output
  • Forgetting that partition returns Pair of lists
  • Confusing startsWith with contains
4. Identify the error in this Kotlin code using partition:
val nums = listOf(1, 2, 3, 4)
val evens, odds = nums.partition { it % 2 == 0 }
println(evens)
println(odds)
medium
A. partition cannot be used on lists of integers.
B. The lambda condition is incorrect; should be it % 2 != 0.
C. Missing parentheses around evens and odds for destructuring declaration.
D. println statements are missing string interpolation.

Solution

  1. Step 1: Check destructuring declaration syntax

    Destructuring a Pair requires parentheses: val (evens, odds) = ....
  2. Step 2: Confirm lambda and usage

    The lambda is correct for evens; partition works on lists of integers; println is fine.
  3. Final Answer:

    Missing parentheses around evens and odds for destructuring declaration. -> Option C
  4. Quick Check:

    Destructuring needs parentheses [OK]
Hint: Use val (a, b) = ... for destructuring Pair [OK]
Common Mistakes:
  • Omitting parentheses in destructuring
  • Changing correct lambda unnecessarily
  • Thinking partition only works on strings
5. Given a list of mixed integers including zero and negatives, how can you use partition to separate positive numbers from zero and negatives? Choose the correct code snippet.
hard
A. val (positives, nonPositives) = numbers.partition { it < 0 }
B. val (positives, nonPositives) = numbers.partition { it > 0 }
C. val (positives, nonPositives) = numbers.partition { it >= 0 }
D. val (positives, nonPositives) = numbers.partition { it == 0 }

Solution

  1. Step 1: Define the condition for positives

    Positive numbers are greater than zero, so condition is it > 0.
  2. Step 2: Use partition with this condition

    Partition splits list into positives and the rest (zero and negatives).
  3. Final Answer:

    val (positives, nonPositives) = numbers.partition { it > 0 } -> Option B
  4. Quick Check:

    Partition by it > 0 separates positives [OK]
Hint: Use it > 0 to get positives, rest are zero/negatives [OK]
Common Mistakes:
  • Using it >= 0 includes zero as positive
  • Using it < 0 reverses groups
  • Using it == 0 only selects zeros
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard