Isn't `all_groups` already a chain object? I think `itertools.chain(all_groups)` is not necessary.