title: How to wrap around to the start/end of a list?
body: <p>I have a 2d array with a different species in each one. I pick a random element on the array and I want to count up how many of each species are in the eight squares immediately adjacent to that element.</p>
  
  <p>But I want the array to wrap at the edges, so if I pick an element on the top row, the bottom row will be counted as "adjacent". How can I do this while iterating through <code>j in range (x-1,x+1)</code> and the same for j and y?</p>
  
  <p>Also, is there a more elegant way of omitting the element I originally picked while looking through the adjacent squares than the <code>if (j!=x or k!=y</code> line?</p>
  
  <pre><code>numspec = [0] * len(allspec)
  for i in range (0,len(allspec)):
      #count up how many of species i there is in the immediate area
      for j in range(x-1,x+1):
          for k in range(y-1,y+1):
              if (j!=x or k!=y):
                  numspec[hab[i][j]] = numspec[hab[i][j]]+1
  </code></pre>