Kotlin List.drop() Function
The Kotlin List.drop() function is used to delete first n elements from this List. n is passed as argument to drop() method. drop() method does not modify the original list, but creates a new list and returns it.
Syntax of List.drop()
The syntax of List.drop() function is
</>
Copy
fun <T> List<T>.drop(n: Int): List<T>
Returns
List.
Examples
In this example, we take a list with five elements. We drop the first two elements using drop() function.
Kotlin Program
</>
Copy
fun main(args: Array<String>) {
var list = listOf(2, 4, 6, 8, 10)
var result = list.drop(2)
println(result)
}
Output
[6, 8, 10]
If the n passed as argument to drop() is greater than the length of list, then drop() returns an empty list.
Kotlin Program
</>
Copy
fun main(args: Array<String>) {
var list = listOf(2, 4, 6, 8, 10)
var result = list.drop(7)
println(result)
}
Output
[]
Conclusion
In this Kotlin Tutorial, we learned how to drop/delete first n elements of given list, using Kotlin List.drop() function.