We start with a list, and we wish to construct a generator that yields each element of the list exactly once, but in random order.
In other words, an iterator version of random.shuffle(list)
The motivation is the following: suppose you have a huge list and wish to extract one random element that satisfies a certain property. Typically, you expect many elements to have the property, so this approach:
-Select a random element, check if it has the property. If that is the case, yield the element, otherwise, loop
is more efficient that this other one:
-Filter the list and select one element.
However the first approach will have find trouble if none, or few elements have the property if your iteration of the list does not guarantee that every element is yielded at least once, and will not be efficient if the iteration does not guarantee that every element is yielded at most once.
Thanks everybody