-1

I have a list of 20 products and want to generate the list of all possible combinations of 4 unique products. the order is not important example (a,b,c,d) = (b,d,a,c)... and only one should be listed.

Thank you

Ned

I'm not a programmer and a beginner trying to learn Python.

I calculated 4845 combinations but would like to use python to generate them.

Ned
  • 1

1 Answers1

0

This can be solve using the itertools package, using itertools.combinations(iterable, r) where iterable is the list of elements that you want to calculate the comb, and r is the length subsequences of elements

import itertools

# You define the list of your products
products = [1, 2, 3, 4, 5, 6, 7,]

comb = list(itertools.combinations(products, 4))

If you want to guarantee that there is not any repetition:

import itertools

# You define the list of your products
products = [1, 2, 3, 4, 5, 6, 7,]

# This will remove the duplicate products
products = list(set(products))

comb = list(itertools.combinations(products, 4))

Please refer to https://docs.python.org/3/library/itertools.html#itertools.combinations