When it is required to find all the pair combinations between two tuples, the list comprehension can be used.
Below is the demonstration of the same −
from itertools import product N = 2 print("The value of N has been initialized to ") print(N) my_result = [ele for ele in product(range(1, N + 1), repeat = N)] print("All tuple combinations until 2 are : " ) print(my_result)
The value of N has been initialized to 2 All tuple combinations until 2 are : [(1, 1), (1, 2), (2, 1), (2, 2)]
The required packages are imported.
The value of N is set and is displayed on the console.
The list comprehension is used to iterate over the values up to N, and it is incremented.
This is assigned to a variable.
It is displayed as output on the console.