
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
All Pair Combinations of 2 Tuples in Python
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
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.
Advertisements