Skip to content
Related Articles
Open in App
Not now

Related Articles

Tuple Division in Python

Improve Article
Save Article
  • Last Updated : 07 Feb, 2023
Improve Article
Save Article

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))

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():

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

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)


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!