from __future__ import print_function import random def pick_random_elements(xs, n): for i in range(min(n, len(xs))): i1 = random.randrange(i, len(xs)) yield xs[i1] xs[i1] = xs[i]