from typing import Dict
from typing import Optional
import io
import csv


def create_csv_from_data(
    data: Dict, stream: Optional["io.StringIO"] = None
) -> "io.StringIO":
    """Creates a CSV stream using the given dict, in where the keys are
    the columns and each value is a list of results.
    """
    stream = io.StringIO() if not stream else stream
    header = list(data.keys())
    writer = csv.DictWriter(stream, fieldnames=data.keys())
    writer.writeheader()
    for index in range(len(data[header[0]])):
        row = {key: data[key][index] for key in header}
        writer.writerow(row)
    return stream