import itertools
import random


def reservoir(iterator, k):
    """ Performs reservoir sampling of k items in iterator. Make sure that the iterator is a once-only iterator
    (ie. not created using the "range" function).

    :param iterator: set of items to sample from
    :param k: sample k items
    :return: list of sampled items
    """
    sample = list(itertools.islice(iterator, 0, k))
    for i, item in enumerate(iterator):
        replace = random.randint(0, i + k)
        if replace < k:
            sample[replace] = item
    return sample