import functools
import operator


def condense(groups):
    """Condense the output list. some groups that appear are subset of
    another group. for example, the grouping output can be:

    ( ('A', 'B'),
      ('A', 'B', 'C'),
      ('A', 'B', 'C', 'D', 'E'),
      ('C', 'D', 'E', 'F'),
      ('G', 'H', 'I'))

    the condense() function will condense the above output to the
    following:

    ( ('G', 'H', 'I'),
      ('C', 'D', 'E', 'F'),
      ('A', 'B', 'C', 'D', 'E'))
    """
    group_set = [set(x) for x in groups]
    group_sorted = sorted(group_set, key=lambda x: len(x))

    condensed = []

    for i in range(len(group_sorted)):

        item = group_sorted[i]
        rest = group_sorted[i+1:]

        if len(rest) == 0:
            rest = group_sorted[:i]

        subset_check = [item.issubset(x) for x in rest]
        if not functools.reduce(operator.or_, subset_check, False):
            condensed.append(tuple(sorted(list(item))))

    return tuple(condensed)