listscalafilterscala-2.10

How to remove an item from a list in Scala having only its index?


I have a list as follows:

val internalIdList: List[Int] = List()

internalIdList = List(11, 12, 13, 14, 15)

From this list would remove the third element in order to obtain:

internalIdList = List(11, 12, 14, 15)

I can not use a ListBuffer, are obliged to maintain the existing structure. How can I do?

Thanks to all


Solution

  • Simply use

    val trunced = internalIdList.take(index) ++ internalIdList.drop(index + 1)
    

    This will also work if index is larger than the size of the list (It will return the same list).