fork download
  1. elements = [1,2,3,4,5,6]
  2. combinations = []
  3. k = 3
  4.  
  5. def get_combinations(elem_idx, current_set):
  6.  
  7. if elem_idx == len(elements):
  8. return
  9.  
  10. new_set = current_set[:]
  11. new_set.append(elements[elem_idx])
  12.  
  13. if len(new_set) == k:
  14. combinations.append(new_set)
  15.  
  16. get_combinations(elem_idx+1, current_set)
  17. get_combinations(elem_idx+1, new_set)
  18.  
  19.  
  20. get_combinations(0, [])
  21.  
  22. print(combinations)
  23.  
Success #stdin #stdout 0.03s 118784KB
stdin
Standard input is empty
stdout
[[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]]