mapPartition
should be thought of as a map operation over partitions and not over the elements of the partition. It's input is the set of current partitions its output will be another set of partitions.
The function you pass to map
operation must take an individual element of your RDD
The function you pass to mapPartition
must take an iterable of your RDD type and return an iterable of some other or the same type.
In your case you probably just want to do something like:
def filter_out_2(line):
return [x for x in line if x != 2]
filtered_lists = data.map(filterOut2)
If you wanted to use mapPartition
it would be:
def filter_out_2_from_partition(list_of_lists):
final_iterator = []
for sub_list in list_of_lists:
final_iterator.append( [x for x in sub_list if x != 2])
return iter(final_iterator)
filtered_lists = data.mapPartition(filterOut2FromPartion)