Oksana Ok
Oksana Ok

Reputation: 555

2 value , 3 spaces - how to loop through to return all different variations

If I have 2 values:

  1. X and Y

And 3 spaces - ["","",""]

How to create a looping mechanism to return all different variations?

E.g.

Variation 1 = [X,X,X]
Variation 2 = [X,X,Y]
.....
Variation .. = [Y,Y,Y]

Upvotes: 1

Views: 39

Answers (1)

Ashish M J
Ashish M J

Reputation: 700

The simplest way to do this is itertools.product(). It allows you to create what you want in a simple one-liner:

import itertools

a=[]
X=1
Y=2
a.append(X)
a.append(Y)

combinations = list(itertools.product(a,repeat=3))

print(combinations)

Output

[(1, 1, 1), (1, 1, 2), (1, 2, 1), (1, 2, 2), (2, 1, 1), (2, 1, 2), (2, 2, 1), (2, 2, 2)]

Upvotes: 2

Related Questions