This article explores different ways to iterate from the second item of a List in Kotlin.

1. Using subList() function

A simple solution is to use the subList() function to iterate through the sublist starting from the second element till the end of the list.

Download Code

Output:

2
3
4
5

 
The subList() function just returns a “view” of the original list and does not create an intermediary list. Note that the starting index is inclusive, and the ending index is exclusive. Here’s an equivalent version using the forEach function:

Download Code

Output:

2
3
4
5

2. Using drop() function

Alternatively, you can call the drop() function to discard the first element of the list, and process the remaining elements. This is demonstrated below:

Download Code

Output:

2
3
4
5

3. Using List Iterator

Another option to iterate over the elements in the list is to create a ListIterator starting from the second position of the list. For instance,

Download Code

Output:

2
3
4
5

4. Using Ranges

Kotlin lets you easily create Integral type ranges, which can be iterated over. The idea is to create a range of list indices between the second element and the last, as shown below:

Download Code

Output:

2
3
4
5

 
Alternatively, you can use the operator form .. of ranges, with the lastIndex:

Download Code

Output:

2
3
4
5

That’s all about iterating from the second item of a List in Kotlin.