I get the whole list:

private fun collectParentItems(): List<Any> { val parentItems = collectParentCategoriesGuids().map { CategoriesListItem(it) } return if (parentItems.isEmpty()) emptyList() else parentItems } 

How can I still get the whole list, but exclude one (specific) element from it?

  • Formulate a condition how to define what exactly should be excluded. - Eugene Krivenja

1 answer 1

This can be done with the help of the overloaded operator - , which copies the sequence to a new list, excluding the first occurrence of this element:

 val list = listOf(1, 2, 3, 1, 2, 3) val result = list - 2 println(result) // [1, 3, 1, 2, 3] 

(run example)

  • Well, specifically, in my example, how can this be implemented? I need to remove the string, but unfortunately, according to your method, it doesn’t quite work out ... - Inkognito
  • @Inkognito, that is, you need to exclude from the list the item that was created as CategoriesListItem(x) , knowing x ? - hotkey