def _discard_key_and_flatten_values(rdd, sort_values=False):
    """Helper function for :py:func:`_run_combiner` and
    :py:func:`_shuffle_and_sort`.

    Given an RDD containing (key, [line1, line2, ...]), discard *key*
    and return an RDD containing line1, line2, ...

    Guarantees that lines in the same list will end up in the same partition.

    If *sort_values* is true, sort each list of lines before flattening it.
    """
    if sort_values:
        map_f = lambda key_and_lines: sorted(key_and_lines[1])
    else:
        map_f = lambda key_and_lines: key_and_lines[1]
    return rdd.flatMap(map_f, preservesPartitioning=True)
