def indices(N, length=1):
    """Generate [length]-tuples of indices.
    Each tuple t = (i, j, ..., [x]) satisfies the conditions 
    len(t) == length, 0 <= i < N  and i > j > ... > [x].
    Arguments:
      N (int): The limit of the first index in each tuple.
      length (int, optional): The length of each tuple (defaults to 1).
    Yields:
      tuple: The next tuple of indices.
    """
if length == 1:
    for x in range(N):
        yield x,
else:
    for x in range(1, N):
        for t in indices(x, length - 1):
            yield (x,) + t
Итерация по нескольким индексам с i> j (> k) питоническим способом