Getting the first item for a tuple for eaching a row in a list in pyspark
<p>I'm a bit new to Spark and I am trying to do a simple mapping.<br>
My data is like the following:</p>

<pre><code>RDD((0, list(tuples)), ..., (19, list(tuples))
</code></pre>

<p>What I want to do is grabbing the first item in each tuple, so ultimately something like this: </p>

<pre><code>RDD((0, list(first item of each tuple),..., (19, list(first item of each tuple))
</code></pre>

<p>Can someone help me out with how to map this?<br>
I'll appreciate that! </p>

<p>Something like this? </p>

<p><code>kv</code> here meaning "key-value" and mapping <a href="https://docs.python.org/3/library/operator.html#operator.itemgetter" rel="nofollow"><code>itemgetter</code></a> over the values. So, <code>map</code> within a <code>map</code> :-)</p>

<pre><code>from operator import itemgetter
rdd = sc.parallelize([(0, [(0,'a'), (1,'b'), (2,'c')]), (1, [(3,'x'), (5,'y'), (6,'z')])])
mapped = rdd.mapValues(lambda v: map(itemgetter(0), v))
</code></pre>

<p>Output</p>

<pre><code>mapped.collect()
[(0, [0, 1, 2]), (1, [3, 5, 6])]
</code></pre>

python apache-spark pyspark