elements = [1,2,3,4,5,6] combinations = [] k = 3 def get_combinations_BFS(): queue = [([],0)] while queue: combination, elem_idx = queue.pop() if elem_idx == len(elements): continue new_comb = combination[:] new_comb.append(elements[elem_idx]) if len(new_comb) == k: combinations.append(new_comb) queue.append((combination, elem_idx+1)) queue.append((new_comb, elem_idx+1)) get_combinations_BFS() print(combinations)
Standard input is empty
[[1, 2, 3], [1, 2, 4], [1, 2, 5], [1, 2, 6], [1, 3, 4], [1, 3, 5], [1, 3, 6], [1, 4, 5], [1, 4, 6], [1, 5, 6], [2, 3, 4], [2, 3, 5], [2, 3, 6], [2, 4, 5], [2, 4, 6], [2, 5, 6], [3, 4, 5], [3, 4, 6], [3, 5, 6], [4, 5, 6]]