I want to add other elements to a list in the index of a given item. for instance
val list = List(1,2,3,4)
I want to replace the element 3 with (3, 7) which will gives this output
val list = List(1,2,3,7,4)
in an efficient and generic way
here is what I tried
list.zipWithIndex.map { case (item, index) =>
if(item == 3) {
list.updated(index, List(item ,7))
} else list
but this is not working and its not efficient in case of traversing a long list
list.take(index) ++ List(7) ++ list.drop(index)
See answer linked in comments for more efficient version using splitAt