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
Step
Element
Predicate (it % 2 == 0)
Add to Even List?
Add to Odd List?
1
1
false
No
Yes
2
2
true
Yes
No
3
3
false
No
Yes
4
4
true
Yes
No
5
5
false
No
Yes
End
-
-
Partition complete
Partition complete
💡 All elements checked, partition returns Pair(evenList, oddList)
Variable Tracker
Variable
Start
After 1
After 2
After 3
After 4
After 5
Final
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.