In previous articles, we learned about various functions and operations that we can perform on Tuples in Python. In this article we will work on practice programs related to Python Tuples.
Program – Problem Statement
We will be given two tuples with number elements. We need to create all unique pair combinations from these two tuple elements. Output will be a nested tuple with each element is also a tuple of two numbers with each number from given two tuples. Each element of output nested tuple should be unique without inclusion of order of elements.
Two tuples with same element numbers but different order will not be equal. e.g. Two tuples (10, 20) and (20, 10) has same element numbers but order of elements is different. So, these two tuples are not equal.
Test Case 1
Input: input_tuple1 = (10, 20), input_tuple2 = (30, 40)
Output: ((10, 30), (10, 40), (20, 30), (20, 40), (30, 10), (30, 20), (40, 10), (40, 20))
Test case explanation:
Two tuples with number elements will be given as an input. We will create all unique pair combinations from these two tuple elements. e.g. From the first tuple we take the first element i.e. 10. Pair it with each element in second tuple and create paired tuples i.e. (10, 30) & (10, 40). Add these two paired tuples in the output nested tuple. Similar process followed with the other elements in first tuple and created paired elements with the second tuple i.e. (20, 30) & (20, 30). Note – In these paired tuples, first element was from first tuple and second element was from second tuple.
Now reverse the order of paired tuple elements. Use second tuple as the first element and first tuple as second element. It will give paired tuples with reverse order of elements i.e. (30, 10), (30, 20), (40, 10), (40, 20). Add all these paired tuples in the nested tuple. Nested tuple is returned as an output.
Program – Solution
Solution Program Steps
- Take two variables ‘input_tuple1’ and ‘input_tuple2’ to reference given two input tuples.
- Input tuples can have duplicate elements and if not removed can create duplicate paired tuples. First we need to remove the duplicate elements from both input tuples. Since, set objects will not store duplicate elements in it, We can convert tuple into a set and then convert set back to tuple. OR we can use loop to remove duplicate elements from the tuple.
- Define a empty list as ‘output_list’ to store paired tuples.
- Using for loop, iterate through the elements of unique_input_tuple1 and unique_input_tuple2. Create paired tuples combining one element from unique_input_tuple1 and one element from unique_input_tuple2.
- Append paired tuples into the ‘output_list’.
- Follow similar steps to create paired tuple with reverse order of elements. Using for loop, iterate through the elements of unique_input_tuple2 and unique_input_tuple1. Create paired tuples combining one element from unique_input_tuple2 and one element from unique_input_tuple1.
- Note – We can have paired tuples in which both elements are same i.e. (10, 10) or (12, 12). To avoid duplicate of these paired tuples in reverse order we need to make an additional check. We will check if the element from the ‘unique_input_tuple2’ is not equal to element from ‘unique_input_tuple1’.
- Append paired tuples into the ‘output_list’.
- As an output, convert the ‘output_list’ into a tuple and print it as an output.
Solution Code 1 – Remove duplicate using ‘set’
input_tuple1 = (6, 8, 8)
input_tuple2 = (2, 4, 5, 5)
unique_input_tuple1 = tuple(set(input_tuple1)) # Remove duplicate using set
unique_input_tuple2 = tuple(set(input_tuple2))
output_list = []
for m in unique_input_tuple1:
for n in unique_input_tuple2:
output_list.append(tuple((m, n))) # create and append paired tuple
for n in unique_input_tuple2:
for m in unique_input_tuple1:
if m != n: # conditional check to avoid same number paired tuple
output_list.append(tuple((n, m))) # create and append paired tuple in reverse order
print(tuple(output_list))
Test cases
Test Case 1
Input: input_tuple1 = (10, 20), input_tuple2 = (30, 40)
Output: ((10, 30), (10, 40), (20, 30), (20, 40), (30, 10), (30, 20), (40, 10), (40, 20))
Test Case 2
Input: input_tuple1 = (3, 5), input_tuple2 = (3, 8)
Output: ((3, 3), (3, 8), (5, 3), (5, 8), (3, 5), (8, 3), (8, 5))
Test Case 3
Input: input_tuple1 = (6, 8, 8), input_tuple2 = (2, 4, 5, 5)
Output: ((6, 2), (6, 4), (6, 5), (8, 2), (8, 4), (8, 5), (2, 6), (2, 8), (4, 6), (4, 8), (5, 6), (5, 8))
Solution Code 2 – Remove duplicate using loop
input_tuple1 = (6, 8, 8)
input_tuple2 = (2, 4, 5, 5)
unique_temp_list = []
for x in input_tuple1:
if x not in unique_temp_list:
unique_temp_list.append(x)
unique_input_tuple1 = tuple(unique_temp_list) # Removed duplicate from tuple1
unique_temp_list = []
for x in input_tuple2:
if x not in unique_temp_list:
unique_temp_list.append(x)
unique_input_tuple2 = tuple(unique_temp_list) # Removed duplicate from tuple2
output_list = []
for m in unique_input_tuple1:
for n in unique_input_tuple2:
output_list.append(tuple((m, n))) # create and append paired tuple
for n in unique_input_tuple2:
for m in unique_input_tuple1:
if m != n: # conditional check to avoid same number paired tuple
output_list.append(tuple((n, m))) # create and append paired tuple in reverse order
print(tuple(output_list))
Summary
In this article, we write a Python program for a problem statement related to Python tuples and following points were covered.
Code – Github Repository
All code snippets and programs for this article and for Python tutorial, can be accessed from Github repository – Comments and Docstring in Python.