Remove data from list while iterating kotlin : <p>I am new to kotlin programming. What I want is that I want to remove a particular data from a list while iterating through it, but when I am doing that my app is crashing. </p>

<pre><code>for ((pos, i) in listTotal!!.withIndex()) {

            if (pos != 0 &amp;&amp; pos != listTotal!!.size - 1) {

                if (paymentsAndTagsModel.tagName == i.header) {
                    //listTotal!!.removeAt(pos)
                    listTotal!!.remove(i)
                }



            }
        }
</code></pre>

<p>OR</p>

<pre><code> for ((pos,i) in listTotal!!.listIterator().withIndex()){
            if (i.header == paymentsAndTagsModel.tagName){
                listTotal!!.listIterator(pos).remove()
            }

        }
</code></pre>

<p>The exception which I am getting</p>

<pre><code>java.lang.IllegalStateException
</code></pre>
