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().
Python3
# Python3 code to demonstrate working of
# Tuple division
# using zip() + generator expression
# initialize tuples
test_tup1 = (10, 4, 6, 9)
test_tup2 = (5, 2, 3, 3)
# printing original tuples
print("The original tuple 1 : " + str(test_tup1))
print("The original tuple 2 : " + str(test_tup2))
# Tuple division
# using zip() + generator expression
res = tuple(ele1 // ele2 for ele1, ele2 in zip(test_tup1, test_tup2))
# printing result
print("The divided tuple : " + str(res))
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().
Python3
# Python3 code to demonstrate working of
# Tuple division
# using map() + floordiv
from operator import floordiv
# initialize tuples
test_tup1 = (10, 4, 6, 9)
test_tup2 = (5, 2, 3, 3)
# printing original tuples
print("The original tuple 1 : " + str(test_tup1))
print("The original tuple 2 : " + str(test_tup2))
# Tuple division
# using map() + floordiv
res = tuple(map(floordiv, test_tup1, test_tup2))
# printing result
print("The divided tuple : " + str(res))
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
Python3
# Python3 code to demonstrate working of
# Tuple division
# initialize tuples
test_tup1 = (10, 4, 6, 9)
test_tup2 = (5, 2, 3, 3)
# printing original tuples
print("The original tuple 1 : " + str(test_tup1))
print("The original tuple 2 : " + str(test_tup2))
# Tuple division
res=[]
for i in range(0,len(test_tup1)):
res.append(test_tup1[i]//test_tup2[i])
res=tuple(res)
# printing result
print("The divided tuple : " + str(res))
OutputThe 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():
Python3
test_tup1 = (10, 4, 6, 9)
test_tup2 = (5, 2, 3, 3)
print("The original tuple 1 : ", test_tup1)
print("The original tuple 2 : ", test_tup2)
res = []
for ele1, ele2 in zip(test_tup1, test_tup2):
res.append(ele1 // ele2)
res = tuple(res)
print("The divided tuple : ", res)
#This code is contributed by Jyothi pinjala
OutputThe 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.
Python3
test_tup1 = (10, 4, 6, 9)
test_tup2 = (5, 2, 3, 3)
# Compute the floor division of corresponding elements
# in the two tuples using a generator expression
res = tuple(ele1 // ele2 for ele1, ele2 in zip(test_tup1, test_tup2))
# Print the original tuples and the resulting tuple
print("The original tuple 1 : ", test_tup1)
print("The original tuple 2 : ", test_tup2)
print("The divided tuple : ", res)
OutputThe 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.
Python3
import numpy as np
test_tup1 = (10, 4, 6, 9)
test_tup2 = (5, 2, 3, 3)
# Compute the floor division of corresponding elements
# in the two tuples using numpy's floor_divide function
res = np.floor_divide(test_tup1, test_tup2)
# Print the original tuples and the resulting tuple
print("The original tuple 1 : ", test_tup1)
print("The original tuple 2 : ", test_tup2)
print("The divided tuple : ", res)
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.
Python3
# Import libraries
import functools
import operator
# Define input tuples
test_tup1 = (10, 4, 6, 9)
test_tup2 = (5, 2, 3, 3)
# Apply operator.floordiv() using reduce()
res = tuple(functools.reduce(operator.floordiv, pair) for pair in zip(test_tup1, test_tup2))
# Print the original tuples and the resulting tuple
print("The original tuple 1 : ", test_tup1)
print("The original tuple 2 : ", test_tup2)
print("The divided tuple : ", res)
OutputThe 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.
Similar Reads
Floor Division in Python
Division Operators allow you to divide two numbers and return a quotient, i.e., the first number or number at the left is divided by the second number or number at the right and returns the quotient. This article will explain how to execute floor division in Python.Example: Floor Division with Integ
3 min read
Python - Dividing two lists
Dividing two lists in Python means performing division between elements at the same position in both lists. Each element in the first list is divided by the corresponding element in the second list to create a new list of results. In this article, we will explore How to Divide two lists. Using NumPy
3 min read
Python Access Tuple Item
In Python, a tuple is a collection of ordered, immutable elements. Accessing the items in a tuple is easy and in this article, we'll explore how to access tuple elements in python using different methods.Access Tuple Items by IndexJust like lists, tuples are indexed in Python. The indexing starts fr
2 min read
Python - Dictionary Values Division
Sometimes, while working with dictionaries, we might have utility problem in which we need to perform elementary operation among the common keys of dictionaries. This can be extended to any operation to be performed. Letâs discuss division of like key values and ways to solve it in this article. Met
5 min read
Python | Divide constant to Kth Tuple index
Many times, while working with records, we can have a problem in which we need to change the value of tuple elements. This is a common problem while working with tuples. Letâs discuss certain ways in which N can be divided by Kth element of tuple in list. Method #1: Using loop Using loops this task
4 min read
Python - Consecutive Division in List
Given a List, perform consecutive division from each quotient obtained in the intermediate step and treating consecutive elements as divisors. Input : test_list = [1000, 50, 5, 10, 2] Output : 0.2 Explanation : 1000 / 50 = 20 / 5 = 4 / 10 = 0.4 / 2 = 0.2. Hence solution. Input : test_list = [100, 50
2 min read
Python - Convert Binary tuple to Integer
Given Binary Tuple representing binary representation of a number, convert to integer. Input : test_tup = (1, 1, 0) Output : 6 Explanation : 4 + 2 = 6. Input : test_tup = (1, 1, 1) Output : 7 Explanation : 4 + 2 + 1 = 7. Method #1 : Using join() + list comprehension + int() In this, we concatenate t
5 min read
Tuple within a Tuple in Python
In the realm of Python programming, tuples offer a flexible solution for organizing data effectively. By allowing the inclusion of other tuples within them, tuples further enhance their capabilities. In this article, we will see how we can use tuple within a tuple in Python. Using Tuple within a Tup
4 min read
Python - Modulo of tuple elements
Sometimes, while working with records, we can have a problem in which we may need to perform a modulo of 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 the above
8 min read
Convert String to Tuple - Python
When we want to break down a string into its individual characters and store each character as an element in a tuple, we can use the tuple() function directly on the string. Strings in Python are iterable, which means that when we pass a string to the tuple() function, it iterates over each characte
2 min read