That’s not what that loop does, it puts one item into each next group and loops back over the groups after every three items. Really it ought to be a by-three stepped loop over items, inserting each into each group inline:
groups[1], groups[2], groups[3] = items[i], items[i+1], items[i+2]
If the group count is dynamic, you can just loop over groups instead, and then step through items by #groups, inserting.