Reputation: 555
If I have 2 values:
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
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