4

say I have 3 different variables and each has 2 possible values, so in total I have 8 different combinations. Is there a python library function, or an algorithm that I can use to print all possible combinations?

Thanks

3

2 Answers 2

11

I think you're looking for product:

a = [1, 2]
b = [100, 200]
c = [1000, 2000]

import itertools
for p in itertools.product(a, b, c):
    print p

prints:

(1, 100, 1000)
(1, 100, 2000)
(1, 200, 1000)
(1, 200, 2000)
(2, 100, 1000)
(2, 100, 2000)
(2, 200, 1000)
(2, 200, 2000)
Sign up to request clarification or add additional context in comments.

Comments

1

And the true one function here is itertools.product

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.