Open In App

Tuple Division in Python

Last Updated : 05 May, 2023
Comments
Improve
Suggest changes
1 Like
Like
Report

Sometimes, while working with records, we can have a problem in which we may need to perform mathematical division operation across tuples. This problem can occur in day-day programming. Let’s discuss certain ways in which this task can be performed.

Method #1 : Using zip() + generator expression The combination of above functions can be used to perform this task. In this, we perform the task of division using generator expression and mapping index of each tuple is done by zip(). 

Output : 
The original tuple 1 : (10, 4, 6, 9)
The original tuple 2 : (5, 2, 3, 3)
The divided tuple : (2, 2, 2, 3)

Time Complexity: O(n), where n is the length of the lists.
Auxiliary Space: O(n)

Method #2 : Using map() + floordiv The combination of above functionalities can also perform this task. In this, we perform the task of extending logic of division using floordiv and mapping is done by map(). 

Output : 
The original tuple 1 : (10, 4, 6, 9)
The original tuple 2 : (5, 2, 3, 3)
The divided tuple : (2, 2, 2, 3)

Time complexity: O(n), where n is the length of the tuple. The code uses the map function which has a time complexity of O(n). 
Auxiliary space: O(n) since a new tuple is created to store the result of the division of the elements of the original tuples.

Method #3 : Using for loop and tuple() method


Output
The original tuple 1 : (10, 4, 6, 9)
The original tuple 2 : (5, 2, 3, 3)
The divided tuple : (2, 2, 2, 3)

Method #4 : Using for loop and zip():


Output
The original tuple 1 :  (10, 4, 6, 9)
The original tuple 2 :  (5, 2, 3, 3)
The divided tuple :  (2, 2, 2, 3)

Time Complexity: O(n)
Auxiliary Space: O(n)

Method 5: Using  list comprehension  + zip()

Use a list comprehension with a generator expression to compute the floor division of corresponding elements from both input tuples. The zip() function can be used to iterate over both tuples in parallel, and the resulting generator can be wrapped in the tuple() constructor to obtain the final result.


Output
The original tuple 1 :  (10, 4, 6, 9)
The original tuple 2 :  (5, 2, 3, 3)
The divided tuple :  (2, 2, 2, 3)

Time Complexity: O(n), where n is the length of the input tuples.
Auxiliary Space: O(n) 

Method #6: Using numpy's floor_divide function

This method uses the numpy library's floor_divide function to perform element-wise floor division of two tuples. The floor_divide function is optimized for numerical operations and can perform the operation more efficiently than regular Python code. However, using numpy requires additional memory usage as numpy arrays are stored in a different format than Python tuples.

Output:

The original tuple 1 :  (10, 4, 6, 9)
The original tuple 2 :  (5, 2, 3, 3)
The divided tuple :  [2 2 2 3]

Time complexity: O(n), where n is the length of the tuples
Auxiliary space: O(n) due to the creation of a numpy array.

Method 7: Using the reduce() function from the functools library

You can also use the reduce() function from the functools library to solve this problem. The reduce() function takes two arguments: a function and a sequence, and applies the function to the sequence in a cumulative way, reducing the sequence to a single value. In this case, we will use the operator.floordiv() function as the function argument.

step-by-step approach:

Import the functools and operator libraries.
Define the input tuples, test_tup1 and test_tup2.
Import the operator.floordiv() function.
Use the reduce() function to apply the operator.floordiv() function to each element of the tuples.
Convert the resulting sequence to a tuple using the tuple() function.
Print the original tuples and the resulting tuple.


Output
The original tuple 1 :  (10, 4, 6, 9)
The original tuple 2 :  (5, 2, 3, 3)
The divided tuple :  (2, 2, 2, 3)

Time complexity: O(n), where n is the length of the input tuples.

Auxiliary space: O(n), where n is the length of the input tuples, as we create a new tuple to store the results.


Practice Tags :

Explore