These functions help you work with parts of a list easily. You can grab some items, skip some, or split the list into smaller groups.
Take, drop, and chunked in Kotlin
val result = list.take(n) val result = list.drop(n) val result = list.chunked(size)
take(n) gets the first n items from the list.
drop(n) skips the first n items and returns the rest.
chunked(size) splits the list into smaller lists each with size items.
val numbers = listOf(1, 2, 3, 4, 5) val firstTwo = numbers.take(2)
val numbers = listOf(1, 2, 3, 4, 5) val afterTwo = numbers.drop(2)
val numbers = listOf(1, 2, 3, 4, 5) val chunks = numbers.chunked(2)
This program shows how to use take, drop, and chunked on a list of fruits.
fun main() { val fruits = listOf("apple", "banana", "cherry", "date", "fig", "grape") val firstThree = fruits.take(3) println("First three fruits: $firstThree") val afterThree = fruits.drop(3) println("Fruits after first three: $afterThree") val groupsOfTwo = fruits.chunked(2) println("Fruits in groups of two: $groupsOfTwo") }
If take(n) asks for more items than the list has, it just returns the whole list.
drop(n) returns an empty list if you drop more items than the list contains.
chunked(size) creates the last chunk with fewer items if the list size isn't a multiple of size.
take(n) gets the first n items from a list.
drop(n) skips the first n items and returns the rest.
chunked(size) splits a list into smaller lists of the given size.
Practice
take(3) do when called on a list?Solution
Step 1: Understand the take function
Thetake(n)function returns the firstnelements from a list.Step 2: Apply to the example
Callingtake(3)returns the first 3 items, not removing or splitting the list.Final Answer:
Returns the first 3 items of the list -> Option AQuick Check:
take(3) = first 3 items [OK]
- Confusing take with drop
- Thinking take removes items
- Mixing take with chunked
myList into chunks of size 4 in Kotlin?Solution
Step 1: Identify the chunked function usage
Thechunked(size)function is called as a method on the list with the chunk size as argument.Step 2: Check each option
OnlymyList.chunked(4)matches the correct syntax. Others are either wrong method or wrong call style.Final Answer:
myList.chunked(4) -> Option DQuick Check:
chunked called as method with size [OK]
- Using chunked as a standalone function
- Confusing chunked with take or drop
- Wrong argument order
val numbers = listOf(10, 20, 30, 40, 50, 60) val result = numbers.drop(2).take(3) println(result)
Solution
Step 1: Apply drop(2) on the list
Dropping first 2 items removes 10 and 20, leaving [30, 40, 50, 60].Step 2: Apply take(3) on the dropped list
Taking first 3 items from [30, 40, 50, 60] gives [30, 40, 50].Final Answer:
[30, 40, 50] -> Option CQuick Check:
drop(2) then take(3) = [30, 40, 50] [OK]
- Taking before dropping
- Off-by-one errors in indexing
- Confusing drop and take order
val list = listOf(1, 2, 3, 4, 5) val chunks = list.chunked(0) println(chunks)
Solution
Step 1: Understand chunked parameter requirements
Thechunked(size)function requires a positive integer size greater than zero.Step 2: Analyze the code
Usingchunked(0)is invalid and will throw an IllegalArgumentException.Final Answer:
chunked size cannot be zero -> Option BQuick Check:
chunked size > 0 [OK]
- Using zero or negative chunk size
- Assuming chunked works like drop
- Ignoring runtime exceptions
val data = listOf(1, 2, 3, 4, 5, 6, 7), which Kotlin expression produces a list of chunks of size 3, but only for the last 6 elements?Solution
Step 1: Identify last 6 elements
Usingdrop(1)removes the first element, leaving last 6 elements [2,3,4,5,6,7].Step 2: Chunk the remaining list
Callingchunked(3)on [2,3,4,5,6,7] splits it into [[2,3,4], [5,6,7]].Final Answer:
data.drop(1).chunked(3) -> Option AQuick Check:
drop(1) then chunked(3) = last 6 in chunks [OK]
- Using take instead of drop for last elements
- Chunking before dropping
- Miscounting chunk sizes
Ready to prove it?
Pass the test and this pattern moves unknown
