title: How to use Python iterators elegantly
body: <p>I am trying to use iterators more for looping since I heard it is faster than index looping. One thing I am not sure is about how to treat the end of the sequence nicely. The way I can think of is to use <code>try</code> and <code>except StopIteration</code>, which looks ugly to me.</p>
  
  <p>To be more concrete, suppose we are asked to print the merged sorted list of two sorted lists <code>a</code> and <code>b</code>. I would write the following</p>
  
  <pre><code>aNull = False
  I = iter(a)
  try:
      tmp = I.next()
  except StopIteration:
      aNull = True
  
  for x in b:
      if aNull:
          print x
      else:
          if x &lt; tmp:
              print x
          else:
              print tmp,x
              try:
                  tmp = I.next()
              except StopIteration:
                  aNull = True
  
  while not aNull:
      print tmp
      try:
          tmp = I.next()
      except StopIteration:
          aNull = True
  </code></pre>
  
  <p>How would you code it to make it neater?</p>