def iterSample(iterable, samplesize):
    results = []
for i, v in enumerate(iterable):
    r = random.randint(0, i)
if r < samplesize:
    if i < samplesize:
        results.insert(r, v)
else:
    results[r] = v
if len(results) < samplesize:
    raise ValueError('Sample larger than population.')
return results
Python random sample with a generator iterable iterator