I would say you probably need to first start by changing your if statement to evaluate the length of the list so:
if len(list) % int(num_groups) == 0:
# Use this to create a list within list that will separate based on number of groups as the sublist size.
groups = [list[i:i+num_groups] for i in range(0, len(list), int(num_groups))
else:
# Whatever you want it to do, you could use the same code above, but if it isn't divisible by the number of groups, you'll possibly end up with a group that has less than the
others
There's probably a couple ways you can go about it, but this seems like a clean way to do it. Here's a StackOverflow post that kind of explains how it works: https://stackoverflow.com/questions/2231663/slicing-a-list-into-a-list-of-sub-lists