Another way to approach this problem is using recursion, e.g.

def find_combinations(items, n, comb, result):
  if n == 0:
    result.append(comb)
  else:
    for item in items:
      find_combinations(items, n - 1, comb + [item], result)

words = []
find_combinations(['a', 'b', 'c'], 3, [], words)
print(words)

For this particular problem it's less efficient than the
technique used by itertools.product, because it generates
sub-combinations multiple times. However, it's a useful
technique to keep in mind whenever you have a "variable
number of nested for-loops" kind of problem.

--
Greg

--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to