def round_robin_schedule(n):
    """
    Generate a round-robin tournament schedule for `n` teams.
    """
m = n + n % 2
for r in range(m - 1):

    def pairing():
        if r < n - 1:
            yield r, n - 1
for i in range(m // 2 - 1):
    p, q = (r + i + 1) % (m - 1), (m + r - i - 2) % (m - 1)
if p < n - 1 and q < n - 1:
    yield p, q
yield list(pairing())
Generating all unique pair permutations