Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a standard Kotlin function to return a list without the item at a given index?

Tags:

kotlin

I need to remove an item from a list without mutating it.

I can write my own:

fun <T> Iterable<T>.withoutItemAt(index: Int): List<T> =
    take(index) + drop(index + 1)

but it feels like this should be in the standard library.

Is there such a function?

like image 511
Duncan McGregor Avatar asked Oct 15 '25 08:10

Duncan McGregor


1 Answers

You could use filterIndexed:

fun <T> Iterable<T>.withoutItemAt(index: Int): List<T> =
    filterIndexed { i, _ -> i != index }
like image 128
Marvin Avatar answered Oct 18 '25 17:10

Marvin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!