How can I alter the index of loop for a list
<p>I've a list and I want to display the content with index by manually changing the value of index. I've tried to do it in two ways but it is not working.</p>

<p>way 1:</p>

<pre><code>x=['hello','how','are','you','hope','you','are','fine','I','am','doing','work','in','python']
for i,item in enumerate(x):
  print (i,item)
  if item == 'are':
    i+=2
</code></pre>

<p>way 2:</p>

<pre><code>x=['hello','how','are','you','hope','you','are','fine','I','am','doing','work','in','python']
for i in xrange(len(x)):
  print x[i]
  if item == 'are':
    i+=2
</code></pre>

<p>Just use a while loop. It's the simplest, clearest and most flexible solution:</p>

<pre><code>i = 0
while i &lt; len(x):
    print x[i]
    if x[i] == 'are':
        i+=1
    i += 1
</code></pre>

<p>As you can see, looping over a string is simple and you are completely free to change the index on the fly -- <code>i</code> is just a normal variable. It's not clear what your code was actually meant to do, so the above does what your code would (presumably) have done if it worked: It will skip the word that <em>follows</em> the word "are".</p>

python python-2.7 nltk