Q: Python 3.7, split set into groups but have a minimum group size Im looking for an efficient way to write a piece of code.
I am making a bot that pairs people off in groups of 4. Everyone signs up (its a discord bot, but that should not effect this portion of the application) by reacting to a message, then the bot takes them and splits them into groups of 4.
But what does the bot do if there are a number of people not divisible by 4?
What I'm looking to do is not allow any less than a group of 3.
12 people = 3 groups of 4
13 people = 3 groups of 3, 1 group of 4
14 people = 2 groups of 3, 2 groups of 4
Does Python have an easy way to do this type of math?
I also am not sure if there is a more efficient way of saying what Im trying to say. If someone is aware of a particular search term I could use, that would be helpful too.

A: Use divide from more_itertools:
from more_itertools import divide

a_list = [range(12), range(13), range(14), range(15), range(16)]

for i in a_list:
    divisor = len(i) // 4
    if len(i) % 4 == 0:
        print([list(j) for j in divide(divisor, i)])
    else:
        print([list(j) for j in divide(4, i)])

[[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]]
[[0, 1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
[[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10], [11, 12, 13]]
[[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11], [12, 13, 14]]
[[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11], [12, 13, 14, 15]]


A: Some simple modulo and integer division math will you get you started.  Assuming there are total_people...
The number of groups of four could be found by
n_full_groups = total_people // 4 # integer division

The remainder can be found with the % operator
n_left_over = total_people % 4

Now you mentioned a requirement to have at least 3 people per group.  if n_left_over equals 3 or 0 you're done.
If n_left_over equals 1 or 2 you would simply subtract one person from one or two of the "full groups" we found above

A: To assign players from a shuffled list to groups of 4 or 3, you can use `playerindex % #of_groups'
import random
from collections import defaultdict
from math import ceil

# parameters
num_players = 15
groupsize = 4
max_group = ceil(num_players / groupsize)

# Setting up test data
player = list(range(num_players)) # player represented by id
random.shuffle(player)
d = defaultdict(list)

# the 'algorithm'
for i,e in enumerate(player):
    d[i % max_group].append(e)

print(list(d.values())) # desired groups as lists of player ids

Out:
[[5, 4, 6, 11], [12, 8, 0, 7], [14, 3, 2, 13], [9, 10, 1]]

