Skip to content
Advertisement

Obtain a list of all combinations of two elements with length n [duplicate]

I have a list of two items: L = [(0), (1)] I would like to obtain a list of lists containing all possible combinations of length n.

For example, with n = 2

JavaScript

I have already tried with combinations of itertools but it doesn’t work if n is greater than the number of elements in L. I found combinations_with_replacement, but with n = 10 it only creates 11 possible combinations, and not 1024 as it should be.

JavaScript

Output:

JavaScript

any suggestions?

Advertisement

Answer

itertools.product is what you want here:

JavaScript

For example:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement