Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Infinite random set generator

This example was created to show that iterators (generators) don't necessarily return a fixed list of values.

import random

def choices(values, **kw):
    while True:
        yield random.choices(values, **kw)

count = 0
for val in choices(['a', 'b', 'c', 'd', 'e', 'f'], k=2):
    print(val)
    count += 1
    if count > 10:
        break

Output: