In Python I can write:
for i, element in enumerate(my_list): print i # the index, starting from 0 print element # the list-element
How can I write this in Kotlin?
There is a forEachIndexed function in the standard library:
forEachIndexed
myList.forEachIndexed { i, element -> println(i) println(element) }
See @s1m0nw1's answer as well, withIndex is also a really nice way to iterate through an Iterable.
withIndex
Iterable
1.4m articles
1.4m replys
5 comments
57.0k users