def iter_sample_fast(iterable, samplesize):
    results = []
iterator = iter(iterable)
try:
    for _ in range(samplesize):
        results.append(next(iterator))
except StopIteration:
    raise ValueError('Sample larger than population.')
random.shuffle(results)
for i, v in enumerate(iterator, samplesize):
    r = random.randint(0, i)
if r < samplesize:
    results[r] = v
return results
Python random sample with a generator iterable iterator