Pythonic approach to iterating through list
<blockquote>
  <p><strong>Possible Duplicate:</strong><br>
  <a href="http://stackoverflow.com/questions/914715/python-looping-through-all-but-the-last-item-of-a-list">Python: Looping through all but the last item of a list</a>  </p>
</blockquote>



<p>Say I want to iterate through a list. Each time I iterate I want to compute something using both the current and next terms. I could do something like</p>



<pre class="lang-python prettyprint-override"><code>mylist = [1, 2, 3, 4]
for i in range(len(mylist)):
    try:
        compute(mylist[i], mylist[i+1])
    except IndexError:
        compute(mylist[i])
</code></pre>

<p>I could also do</p>

<pre class="lang-python prettyprint-override"><code>mylist = [1, 2, 3, 4]
for num in mylist:
    try:
        compute(num, mylist[mylist.index(num)+1])
    except IndexError:
        compute(num)
</code></pre>

<p>Neither of these seems particularly good. Is there a more pythonic approach to doing this?</p>
