import math
def my_func(stud, grps=2):
students_list = [[] for x in range(grps)]
students_amount = math.ceil(len(stud) / grps)
for x, y in zip(range(0,grps), range(0,students_amount * grps,students_amount)):
students_list[x].extend(stud[y:y + students_amount])
if (students_amount - len(students_list[x])) > 0:
students_list[x].extend(['__EMPTY__' for x in range(students_amount - len(students_list[x]))])
return (students_list)
print(my_func(['Anne', 'Diana', 'Gabriele', 'Hannah', 'Inna', 'Luna', 'Maya', 'Nora'], grps=7))
Hi everyone. This is a function, that takes a list with group members and the amount of groups as arguments. The function then calculates an equal number of students per group (rounded up, if it is a fraction number). It returns a new list containing all the groups with it's members first and if needed filling all empty slots with the word __EMPTY__
.
For example, if the parameter grps = 3, then there will be three nested lists containing the 8 group members (3 slots per group or list) and one __EMPTY__
element in the last nested list.
My question is, can this be done differently without using two indexes? My function seems complicated and I wanted to find out, if there are easier solutions for doing this.