elements = [1,2,3,4,5,6] combinations = [] k = 3 def get_combinations(elem_idx, current_set): if elem_idx == len(elements): return new_set = current_set[:] new_set.append(elements[elem_idx]) if len(new_set) == k: combinations.append(new_set) get_combinations(elem_idx+1, current_set) get_combinations(elem_idx+1, new_set) get_combinations(0, []) print(combinations)
Standard input is empty
[[4, 5, 6], [3, 5, 6], [3, 4, 5], [3, 4, 6], [2, 5, 6], [2, 4, 5], [2, 4, 6], [2, 3, 4], [2, 3, 5], [2, 3, 6], [1, 5, 6], [1, 4, 5], [1, 4, 6], [1, 3, 4], [1, 3, 5], [1, 3, 6], [1, 2, 3], [1, 2, 4], [1, 2, 5], [1, 2, 6]]