Python | All possible N combination tuples
Sometimes, while working with Python tuples, we might have a problem in which we need to generate all possible combination pairs till N. This can have application in mathematics domain. Let’s discuss certain ways in which this problem can be solved.
Method #1 : Using list comprehension + product()
This task can be performed using list comprehension which can be used to iterate the container of N numbers and product() performs the task of formation of combinations from them.
# Python3 code to demonstrate working of # All possible N combination tuples # Using list comprehesion + product() from itertools import product # initialize N N = 3 # All possible N combination tuples # Using list comprehesion + product() res = [ele for ele in product(range(1, N + 1), repeat = N)] # printing result print("Tuple Combinations till N are : " + str(res)) |
Tuple Combinations till N are : [(1, 1, 1), (1, 1, 2), (1, 1, 3), (1, 2, 1), (1, 2, 2), (1, 2, 3), (1, 3, 1), (1, 3, 2), (1, 3, 3), (2, 1, 1), (2, 1, 2), (2, 1, 3), (2, 2, 1), (2, 2, 2), (2, 2, 3), (2, 3, 1), (2, 3, 2), (2, 3, 3), (3, 1, 1), (3, 1, 2), (3, 1, 3), (3, 2, 1), (3, 2, 2), (3, 2, 3), (3, 3, 1), (3, 3, 2), (3, 3, 3)]
Method #2 : Using product()
This task can also be performed by using just the single function. The use of list comprehension can be eliminated using the conversion to list.
# Python3 code to demonstrate working of # All possible N combination tuples # Using product() from itertools import product # initialize N N = 3 # All possible N combination tuples # Using product() res = list(product(range(1, N + 1), repeat = N)) # printing result print("Tuple Combinations till N are : " + str(res)) |
Tuple Combinations till N are : [(1, 1, 1), (1, 1, 2), (1, 1, 3), (1, 2, 1), (1, 2, 2), (1, 2, 3), (1, 3, 1), (1, 3, 2), (1, 3, 3), (2, 1, 1), (2, 1, 2), (2, 1, 3), (2, 2, 1), (2, 2, 2), (2, 2, 3), (2, 3, 1), (2, 3, 2), (2, 3, 3), (3, 1, 1), (3, 1, 2), (3, 1, 3), (3, 2, 1), (3, 2, 2), (3, 2, 3), (3, 3, 1), (3, 3, 2), (3, 3, 3)]
Recommended Posts:
- Python | Convert string tuples to list tuples
- Python | Remove tuples having duplicate first value from given list of tuples
- Python | Count tuples occurrence in list of tuples
- Python | Remove tuples from list of tuples if greater than n
- Python | Remove duplicate tuples from list of tuples
- Python | Find the tuples containing the given element from a list of tuples
- Python | Combining tuples in list of tuples
- Python | How to Concatenate tuples to nested tuples
- Permutation and Combination in Python
- Python | Print all string combination from given numbers
- Python | Extract Combination Mapping in two lists
- Python | Get sum of tuples having same first value
- Python Tuples
- Tuples in Python
- Python | Compare tuples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


