from itertools import combinations def solution(buns, reqs): l = [ [] for x in range(buns) ] c = combinations(range(buns), buns-reqs+1) for i,t in enumerate(c): for n in t: l[n].append(i) return l