## 1. Overview

In this tutorial, we’ll look at how to remove elements from a list. Kotlin provides versatile approaches for efficiently removing elements from lists during iteration. We’ll explore distinct techniques tailored to different scenarios.

## 2. Remove Element Using Iterator

Above all, we should mention that removing elements from a list is possible only when the list is mutable. In the case of an immutable list, the compiler throws an error when we try to remove an element from it. The immutable list doesn’t expose methods for modifications.

The first one, a classic approach, involves using an iterator. It allows safe removal during forward iteration:

@Test
fun `when removing element with Iterator then it works`() {
val numbers = mutableListOf(1, 2, 3, 4, 5)
val iterator = numbers.iterator()

while (iterator.hasNext()) {
val element = iterator.next()
if (element % 2 == 0) {
iterator.remove()
}
}
Assertions.assertThat(numbers).containsExactlyElementsOf(mutableListOf(1, 3, 5))
}

In this example and all the next examples, we’ll use a mutable list of numbers as input. We created an iterator, which exposes a remove method. The method removes an element currently pointed by the iterator. Thanks to that, we can safely iterate forward and remove elements.

Moreover, at the end of the test, we check if the list contains only odd numbers. As expected, we removed numbers 2 and 4 from the initial list.

## 3. Remove Elements Using the removeAll() Function

Now, let’s have a look at the removeAll() function. It simplifies the process of removing elements when iterating. Additionally, it provides a concise and readable way to remove elements based on a specified condition.

Let’s show it in an example:

@Test
fun `when removing element with removeAll function then it works`() {
val numbers = mutableListOf(1, 2, 3, 4, 5)
numbers.removeAll { it % 2 == 0 }
Assertions.assertThat(numbers).containsExactlyElementsOf(mutableListOf(1, 3, 5))
}

Thanks to the removeAll function, we provide only the condition of which elements should be removed from the list. Only the odd numbers are left in the list. Moreover, the removeAll function returns true when any element is removed from the collection. Otherwise, it returns false.

## 4. Iterate Over Elements and Remove

Finally, we’ll show how to remove elements when iterating over a list. The direct iteration offers flexibility when removal logic depends on the index or involves more intricate checks.

Most importantly, we must iterate backward. That guarantees that the index of elements won’t change during iteration.

Let’s have a look at an example:

@Test
fun `when removing element iterating then it works`() {
val numbers = mutableListOf(1, 2, 3, 4, 5)
for (element in numbers.reversed()) {
if (element % 2 == 0) {
numbers.remove(element)
}
}
Assertions.assertThat(numbers).containsExactlyElementsOf(mutableListOf(1, 3, 5))
}

Above all, we changed the order of elements when iterating with the reversed function. It returns a read-only list. We use elements of that list to check the condition. Same as in previous examples, we receive a result list of odd numbers.

## 5. Conclusion

In this article, we demonstrated how to remove elements from a list when iterating over it. We used an iterator, the removeAll function, and iterated backward over the list.

As always, the source code of the examples is available over on GitHub.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.