All pair combinations of 2 tuples in Python

PythonServer Side ProgrammingProgramming

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 −

Example

 Live Demo

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)

Output

The value of N has been initialized to
2
All tuple combinations until 2 are :
[(1, 1), (1, 2), (2, 1), (2, 2)]

Explanation

  • 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.

raja
Updated on 17-Apr-2021 13:06:59

Advertisements