title: Is there any way to replace a for loop with something more efficient in python
body: <p>My code below checks surrounding pixels to my object's pixel in python.</p>
  
  <pre><code>self.surr = [None, None, None, None, None, None, None, None]
  
  for i in range(9):
  #for x in range(-1, 2):
      #for y in range(-1, 2):
          if i != 5:
              x = i % 3 - 2
              y = int((i % 3) / 3) - 1
  
          if x == 0 and y == 0:
              pass
          else:
              PAI = allPixels[(self.x + x) % width][(self.y + y) % height] if allPixels[(self.x + x) % width][(self.y + y) % height] != None else None
  
          self.surr[(y * 3) + x] = (PAI)
  
  return self.surr
  </code></pre>
  
  <p>This returns a list of length 8 that holds either a <code>Pixel</code> object or <code>None</code>. allPixels is a 2D array that also holds wither a <code>Pixel</code> object or <code>None</code>.
  I've tried then nested loops that are commented out but they run just a bit slower that the method I'm currently using. This however, is still too slow as if there are 3000 pixels on the screen, which is the lower bounds of the total pixels there will be on the screen in the end, do the maths and you have a LOT going on every frame.</p>
  
  <p>How can I make this run faster using maybe NumPy or some other method? </p>
  
  <p>If you want to see the whole code, it can be found here: <a href="https://pastebin.com/EuutUVjS" rel="nofollow noreferrer">https://pastebin.com/EuutUVjS</a></p>
  
  <p>Thanks for any help you can give me!</p>