pyspark.RDD.zipWithIndex

RDD.zipWithIndex() → pyspark.rdd.RDD[Tuple[T, int]][source]

Zips this RDD with its element indices.

The ordering is first based on the partition index and then the ordering of items within each partition. So the first item in the first partition gets index 0, and the last item in the last partition receives the largest index.

This method needs to trigger a spark job when this RDD contains more than one partitions.

New in version 1.2.0.

Returns
RDD

a RDD containing the zipped key-index pairs

Examples

>>> sc.parallelize(["a", "b", "c", "d"], 3).zipWithIndex().collect()
[('a', 0), ('b', 1), ('c', 2), ('d', 3)]