def grouprobin(iterable, key):
    groups = [list(g) for k, g in groupby(sorted(iterable, key=key), key)]
while groups:
    group = groups.pop(0)
yield group.pop(0)
if group:
    groups.append(group)
How to "round-robin" a single iterable based on some criteria?