title: Previous and next item of list python
body: <p>Can anyone point me towards a practical way of getting the previous and next items of a list. For example:</p>
  
  <pre><code>my_list = [100, 200, 300, 400]
  </code></pre>
  
  <p>Is there a practical way of getting the previous and next item (300 and 100).</p>
  
  <p>I am using <code>my_list[my_list.index(400) - 1]</code> to successfully get the previous item. Python doesn't mind getting a negative index apparently. However, if I try <code>my_list[my_list.index(400) + 1]</code> it doesn't like having an index greater than the number of items in the list. Which makes sense. </p>
  
  <pre><code>Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  IndexError: list index out of range
  </code></pre>
  
  <p>I suppose I can use a loop to loop through and get it - I was just hoping there was a simpler solution. I was thinking along the lines of using <code>iter()</code> but I can't seem to figure out if it is possible to set the index. </p>