What is an efficient way to return previous & next values of a looping list of objects in python?
<p>I have a list of python objects:</p>

<pre><code>fruits = [ 'apple', 'orange', 'banana', 'grape', 'cherry' ]
</code></pre>

<p>I currently have a <code>for</code> loop in a class method that returns "prev_fruit" and "next_fruit" objects for a given object:</p>

<pre><code>def get_prev_next(self, fruit_list):
    prev_fruit = next_fruit = None
    fruit_list_length = len(fruit_list)
    for idx, fruit in enumerate(fruit_list):
        if fruit == self:
            if idx &gt; 0:
                prev_fruit = fruit_list[idx-1]
            if idx &lt; (fruit_list_length-1):
                next_fruit = fruit_list[idx+1]
    return prev_fruit, next_fruit
</code></pre>

<p>This works although there are probably more efficient ways of doing it (which I'm happy to learn about).</p>

<p>I now want the list to optionally be "looping" (previous for first index is last and next index for last is first).</p>

<pre><code>def get_prev_next(self, fruit_list, looping=False):
    ...
</code></pre>

<p>What is an efficient way to do this on lists of objects with 1-10000 values?</p>

<blockquote>
  <p>"efficient" is necessarily not "most efficient" as code legibility and portability is a factor - I don't want to relearn the approach six months from now</p>
</blockquote>