Python ā€“ All Possible N combination tuples


Introduction

In Python, a tuple is one of the types of data structures. The tuples are composed of items or elements and look more like lists. It has elements which cannot be changed after declaration. To return all the possible combinations of the tuple primarily the itertool library is used. The iteration or repetition process through the tuples is done using the iterator object containing various items. For the purpose of returning all possible combinations in a tuple, we can use many functions like combination(), chain(), product() and concatenation method.

All Possible N combination tuples

Tuple is a data structure consisting of elements that are interchangeable after the initialization. The tuples are usually assigned a value and return the statement according to the user's perspective. In the case of returning all the possible combinations of the given tuples are given below with varying methods.

Syntax

combination(substr, N)

The combination() method is used to find the number of ways the string can be chosen and the pair of elements to be displayed is given as N.

Algorithm

  • Step 1 āˆ’ The modules are imported to the code according the need of the function.

  • Step 2 āˆ’ The libraries used in the codes are itertools module to use combination() and product() function.

  • Step 3 āˆ’ The input string is declared as tuple_1 and tuple_2 and which contains a set of string variable in tuple data structure.

  • Step 4 āˆ’ Declare the variable in which pair of elements to be printed.

  • Step 5 āˆ’ To generate all the possible ways in which the ā€˜Nā€™ can be printed are defined using methods like combination(), product() method and append() function.

  • Step 6 āˆ’ The converting value is stores in combined_list.

  • Step 7 āˆ’ The print statement returns all the possible combinations according to the value of N.

Approach

Approach 1 āˆ’ Using combination() method

Approach 2 āˆ’ Using nested loop

Approach 3 āˆ’ Using product() method

Approach 1: Python Program to display tuples using combination() method:

Iter_object is defined and also the length variable ā€œNā€ is defined with a value of 2. This code uses the combination() function to extract all the possible combinations of the given length. The combined tuple is stored in a list called combined_tuple.

Example

#itertools library is imported
import itertools 
#tuple_1 is initialized with list of values
tuple_1 = [3, 1, 7 ,5] 
#num variable is declared as 2
N=2 
#combinations() function is used to combine the list of values in two format
combined_tuples = list(itertools.combinations(tuple_1 ,N))
# It returns the tuples of the iter_object variable
print(combined_tuples)

Output

[(3, 1), (3, 7), (3, 5), (1, 7), (1, 5), (7, 5)]

Approach 2: Python Program to display tuples using a nested loop

The itertool module is not used in this approach and to iterate through the values of the tuple nested for loop is used. Two tuples are initialized with a variable list of values and an empty list is also defined. Using the append() function, we can add the two tuples together and create a new tuple with pair of elements.

Example

#tuple_1 and tuple_2 is initialized with a list of values
tuple_1 = (5, 7)
tuple_2 = (6, 1)
#empty list is initialized
combined_list = []
# nested for loop is used to iterate through the tuples
for one in tuple_1:
   for two in tuple_2:
      combined_list.append((one,two))

# It returns the tuples of the combined_list variable
print("All possible combinations of the tuple are:",combined_list)

Output

All possible combinations of the tuple are: [(5, 6), (5, 1), (7, 6), (7, 1)]

Approach 3: Python Program to display tuples using product() method

The library required for iterating through the tuples is itertools. Two tuples are initialized with a variable list of values. The combining process of two tuples is made easy by using the product() function that undergoes a Cartesian product of the terms and using the print statement to return all the possible values.

Example

#itertools library is imported
import itertools
#tuple_1 and tuple_2 is initialized with a list of values
tuple_1 = (5, 7, 1, 4, 1)
tuple_2 = (6, 8, 2, 0, 2)
#product() function is used to get the combined tuple values
combined_list = list(itertools.product(tuple_1, tuple_2))
# It returns the tuples of the combined_list variable
print("All possible combinations of the tuple are:",combined_list)

Output

All possible combinations of the tuple are: [(5, 6), (5, 8), (5, 2), (5, 0), (5, 2), (7, 6), (7, 8), (7, 2), (7, 0), (7, 2), (1, 6), (1, 8), (1, 2), (1, 0), (1, 2), (4, 6), (4, 8), (4, 2), (4, 0), (4, 2), (1, 6), (1, 8), (1, 2), (1, 0), (1, 2)]

Conclusion

In the Python language, to indicate that you have declared a tuple is done using the brackets ā€œ()ā€. The elements within these brackets can be defined with the elements to initialize as tuples. The advantages of tuples are it follows some specific order in which the elements are defined.

Updated on: 25-Aug-2023

187 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements