Search code examples
pythontuplesrangecombinationsgenerate

python: how can I create something which generates all possible combinations in a form of tuples for integers from A and B


Question:

# Let A= {m | m is an integer satisfying 0 < m < 13} and 
# B = {n | n is an integer satisfying 7 < n < 23}. 

# I'm trying to generate all possible combinations of (A,B).
For example:

A= (1,2,3,4,5,6,7)
B =(7,8,9,10,11...)

Combination = (1,7),(1,8)(1,9)....(2,7),(2,8),(2,9).... and so forth

-I was thinking of using a for loop but could not seem to manage to get it working.


Solution

  • For the sake of simplicity you could just use :

    for a in A:
      for b in B:
        print((a,b,))
    

    or

    combinations = [(a,b,) for a in A for b in B]