[Solved] Splitting a python list into 4 sub-lists that are as even as possible


Try this, it splits the input list into sublists in the round-robin fashion you desire:

def round_robin_sublists(l, n=4):
    lists = [[] for _ in range(n)]
    i = 0
    for elem in l:
        lists[i].append(elem)
        i = (i + 1) % n
    return lists

l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
sublist = round_robin_sublists(l) # [[1, 5, 9], [2, 6, 10], [3, 7], [4, 8]]

    

solved Splitting a python list into 4 sub-lists that are as even as possible