How to randomly partition a list into n nearly equal parts?
Complete 2018 solution (python 3.6):
import random
def partition (list_in, n):
random.shuffle(list_in)
return [list_in[i::n] for i in range(n)]
Beware! this may mutate your original list
shuffle input list.
First you randomize the list and then you split it in n nearly equal parts.
Call random.shuffle()
on the list before partitioning it.