title: Python creating tuple groups in list from another list
body: <p>Let's say I have this data:</p>
  
  <pre><code>data = [1, 2, 3, -4, -5, 3, 2, 4, -2, 5, 6, -5, -1, 1]
  </code></pre>
  
  <p>I need it to be grouped in another list by tuples. One tuple consists of two lists. One for positive numbers, another for negative. And tuples should be created by checking what kind of number it is. Last negative number (I mean in a row that between negative numbers there were no positive ones) means, other numbers must go into another tuple and when it finds another last negative number, it should create another tuple.</p>
  
  <p>So rules are these: All found numbers are being added into first tuple, when it finds negative number, it still adds it to that tuple, till it finds positive number (it means new tuple must be created).</p>
  
  <p>I think it is easier to show, than to explain. After parsing <code>data</code>, the list should look like this:</p>
  
  <pre><code>l = [([1, 2, 3], [-4, -5]), ([3, 2, 4], [-2]), ([5, 6], [-5, -1]), ([1], [])]
  </code></pre>
  
  <p>I created a solution, but I wonder if it's quite optimal. Maybe it is possible to write a more elegant one (and I wonder about performance, is there some better way to write such parser with best possible performance:))?</p>
  
  <pre><code>def neighborhood(iterable):
      iterator = iter(iterable)
      prev = None
      item = iterator.next()  # throws StopIteration if empty.
      for next in iterator:
          yield (prev,item,next)
          prev = item
          item = next
      yield (prev,item,None)
  
  l = []    
  pos = []
  neg = []
  for prev, item, next in neighborhood(data):
      if item &gt; 0:
          pos.append(item)
          if not next:
              l.append((pos, neg))
      else:
          neg.append(item)
          if next &gt; 0:
              l.append((pos, neg))
              pos = []
              neg = []
          elif not next:
              l.append((pos, neg))
  
  print l
  </code></pre>
  
  <p>P.S. <code>if not next</code> part I think can be used only once after main check.</p>