Kotlin List drop()
Syntax & Examples


Syntax of List.drop()

The syntax of List.drop() extension function is:

fun <T> Iterable<T>.drop(n: Int): List<T>

This drop() extension function of List returns a list containing all elements except first n elements.



✐ Examples

1 Example

In this example,

  • We create a list of integers list.
  • We use the drop function with the argument 2 to drop the first two elements from the list.
  • The resulting list result contains elements 3, 4, 5, 6.
  • Finally, we print the result list to standard output using print statement.

Kotlin Program

fun main(args: Array<String>) {
    val list = listOf(1, 2, 3, 4, 5, 6)
    val result = list.drop(2)
    print(result)
}

Output

[3, 4, 5, 6]

2 Example

In this example,

  • We create a list of characters list.
  • We use the drop function with the argument 3 to drop the first three elements from the list.
  • The resulting list result contains elements d.
  • Finally, we print the result list to standard output using print statement.

Kotlin Program

fun main(args: Array<String>) {
    val list = listOf(&quot;a&quot;, &quot;b&quot;, &quot;c&quot;, &quot;d&quot;)
    val result = list.drop(3)
    print(result)
}

Output

[d]

3 Example

In this example,

  • We create a list of strings list.
  • We use the drop function with the argument 1 to drop the first element from the list.
  • The resulting list result contains elements banana, mango, avocado.
  • Finally, we print the result list to standard output using print statement.

Kotlin Program

fun main(args: Array<String>) {
    val list = listOf(&quot;apple&quot;, &quot;banana&quot;, &quot;mango&quot;, &quot;avocado&quot;)
    val result = list.drop(1)
    print(result)
}

Output

[banana, mango, avocado]

Summary

In this Kotlin tutorial, we learned about drop() extension function of List: the syntax and few working examples with output and detailed explanation for each example.