title: Finding neighbors in a list
body: <p>I have a list:</p>
  
  <pre><code>l=['a','&gt;&gt;','b','&gt;&gt;','d','e','f','g','&gt;&gt;','i','&gt;&gt;','&gt;&gt;','j','k','l','&gt;&gt;','&gt;&gt;']
  </code></pre>
  
  <p>I need to extract all the neighbors of <code>'&gt;&gt;'</code> and split them into groups where they have elements in between that are neither <code>'&gt;&gt;'</code> or neigbors of <code>'&gt;&gt;'</code>.</p>
  
  <p>For the example list the expected outcome would be:</p>
  
  <pre><code>[['a', 'b', 'd'], ['g', 'i', 'j'], ['l']]
  </code></pre>
  
  <p>I have tried quite a few things, but all the simple ones have failed one way or another. At the moment the only code that seems to work is this:</p>
  
  <pre><code>def func(L,N):
      outer=[]
      inner=[]
      for i,e in enumerate(L):
          if e!=N:
              try:
                  if L[i-1]==N or L[i+1]==N:
                      inner.append(e)
                  elif len(inner)&gt;0:
                      outer.append(inner)
                      inner=[] 
              except IndexError:
                  pass
      if len(inner):
          outer.append(inner)
      return outer
  
  func(l,'&gt;&gt;')
  
  Out[196]:
  [['a', 'b', 'd'], ['g', 'i', 'j'], ['l']]
  </code></pre>
  
  <p>Although it seems to work, i am wondering if there is a better,cleaner way to do it?</p>