Create an RDD with more elements than its source
<p>I have an RDD called <code>codes</code>, which is a pair, that has a string as its 1st half and another pair as its 2nd half:</p>

<pre><code>In [76]: codes.collect()
Out[76]: 
[(u'3362336966', (6208, 5320)),
 (u'7889466042', (4140, 5268))]
</code></pre>

<p>and I am trying to get this:</p>

<pre><code>In [76]: codes.collect()
Out[76]: 
[(u'3362336966', 6208),
 (u'3362336966', 5320),
 (u'7889466042', 4140),
 (u'7889466042', 5268)]
</code></pre>

<p>How to do this?</p>

<hr>

<p>My failed attempt:</p>

<pre><code>In [77]: codes_in = codes.map(lambda x: (x[0], x[1][0]), (x[0], x[1][1]))
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
&lt;ipython-input-77-e1c7925bc075&gt; in &lt;module&gt;()
----&gt; 1 codes_in = codes.map(lambda x: (x[0], x[1][0]), (x[0], x[1][1]))

NameError: name 'x' is not defined
</code></pre>

<p>I think what you want is the following:</p>

<pre><code>codes_in = codes.map(lambda x: [(x[0], p) for p in x[1]]).flatMap(lambda x: x)
</code></pre>

<p>If it is python 2, for legibility you could:</p>

<pre><code>codes_in = codes.map(lambda k, vs: [(k, v) for v in vs]).flatMap(lambda x: x)
</code></pre>

<p>By this way you will be able to "extract" each value associated with the key and force that every row is a record of form <code>(k, v)</code>.</p>

python apache-spark bigdata distributed-computing rdd