title: R expand.grid() function in Python
body: <p>Is there a Python function similar to the expand.grid() function in R ? Thanks in advance.</p>
  
  <p>(EDIT) Below are the description of this R function and an example.</p>
  
  <pre><code>Create a Data Frame from All Combinations of Factors
  
  Description:
  
       Create a data frame from all combinations of the supplied vectors
       or factors.  
  
  &gt; x &lt;- 1:3
  &gt; y &lt;- 1:3
  &gt; expand.grid(x,y)
    Var1 Var2
  1    1    1
  2    2    1
  3    3    1
  4    1    2
  5    2    2
  6    3    2
  7    1    3
  8    2    3
  9    3    3
  </code></pre>
  
  <p>(EDIT2) Below is an example with the rpy package. I would like to get the same output object but without using R :</p>
  
  <pre><code>&gt;&gt;&gt; from rpy import *
  &gt;&gt;&gt; a = [1,2,3]
  &gt;&gt;&gt; b = [5,7,9]
  &gt;&gt;&gt; r.assign("a",a)
  [1, 2, 3]
  &gt;&gt;&gt; r.assign("b",b)
  [5, 7, 9]
  &gt;&gt;&gt; r("expand.grid(a,b)")
  {'Var1': [1, 2, 3, 1, 2, 3, 1, 2, 3], 'Var2': [5, 5, 5, 7, 7, 7, 9, 9, 9]}
  </code></pre>
  
  <p><strong>EDIT 02/09/2012:</strong> I'm really lost with Python. Lev Levitsky's code given in his answer does not work for me:</p>
  
  <pre><code>&gt;&gt;&gt; a = [1,2,3]
  &gt;&gt;&gt; b = [5,7,9]
  &gt;&gt;&gt; expandgrid(a, b)
  Traceback (most recent call last):
    File "&lt;stdin&gt;", line 1, in &lt;module&gt;
    File "&lt;stdin&gt;", line 2, in expandgrid
  NameError: global name 'itertools' is not defined
  </code></pre>
  
  <p>However the itertools module seems to be installed (typing <code>from itertools import *</code> does not return any error message)</p>