Kotlin List.drop() – Examples

Kotlin List.drop()

The Kotlin List.drop() function returns a list containing all elements except first n elements.

Syntax

List.drop(n)

Example 1

In this example,

  1. Take a list of strings.
  2. Take a value for n, say 3.
  3. Call drop() function on the list, and pass n as argument to the function. The function should return the list with the first three elements dropped from the list, since n is 3.

Program

fun main(args: Array<String>) {
    val list1 = listOf("a", "b", "c", "d", "e")
    val n = 3
    val result = list1.drop(n)
    print(result)
}

Output

[d, e]

Example 2

In this example,

  1. Take a list of strings, with length 5.
  2. Take a value for n, say 10, which is greater than the length of list.
  3. Call drop() function on the list, and pass n as argument to the function. The function should return an empty list, since n is greater than the length of list.

Program

fun main(args: Array<String>) {
    val list1 = listOf("a", "b", "c", "d", "e")
    val n = 10
    val result = list1.drop(n)
    print(result)
}

Output

[]