title: Is there a good way of iterating through several lists(where sometimes they are single valued) in python
body: <p>So basically my question relates to 'zip' (or izip), and this question which was asked before....</p>
  
  <p><a href="https://stackoverflow.com/questions/1919044/is-there-a-better-way-to-iterate-over-two-lists-getting-one-element-from-each-l">Is there a better way to iterate over two lists, getting one element from each list for each iteration?</a></p>
  
  <p>If i have two variables - where they either are a 1d array of values length n, or are a single value, how do i loop through them so that I get n values returned.</p>
  
  <p>'zip' kindof does what I want - except that when I pass in a single value, and an array it complains.  </p>
  
  <p>I have an example of what I'm aiming for below - basically i have a c function that does a more efficient calculation than python.  I want it to act like some of the numpy functions - that deal ok with mixtures of arrays and scalars, so i wrote a python wrapper for it.  However - like I say 'zip' fails.  I guess in principle I can do some testing of the input s and write a different statement for each variation of scalars and arrays - but it seems like python should have something more clever.... ;)  Any advice?</p>
  
  <pre><code>"""
      Example of zip problems.
  """
  
  import numpy as np
  import time
  
  def cfun(a, b) :
      """
          Pretending to be c function which doesn't deal with arrays
      """
      if not np.isscalar(a)   or  not np.isscalar(b)  :
  
          raise Exception('c is freaking out')
      else :
  
          return a+b
  
  def pyfun(a, b) :
      """
          Python Wrappper - to deal with arrays input
      """
  
      if not np.isscalar(a)   or  not np.isscalar(b) :
          return np.array([cfun(a_i,b_i) for a_i, b_i in zip(a,b)])
  
      else :
  
          return cfun(a, b)
  
      return cfun(a,b)
  
  
  a = np.array([1,2])
  b= np.array([1,2])
  print pyfun(a, b)
  
  a = [1,2]
  b = 1
  print pyfun(a, b)
  </code></pre>
  
  <p><strong>edit :</strong></p>
  
  <p>Many thanks everyone for the suggestions everyone.  Think i have to go for np.braodcast for the solution - since it seems the simplest from my perspective.....</p>