Python - Modulo of tuple elements
Last Updated :
08 May, 2023
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 functions can be used to perform this task. In this, we perform the task of modulo using generator expression and the mapping index of each tuple is done by zip().
Python3
# Python3 code to demonstrate working of
# Tuple modulo
# using zip() + generator expression
# Initialize tuples
test_tup1 = (10, 4, 5, 6)
test_tup2 = (5, 6, 7, 5)
# Printing original tuples
print("The original tuple 1 : " + str(test_tup1))
print("The original tuple 2 : " + str(test_tup2))
# Tuple modulo
# using zip() + generator expression
res = tuple(ele1 % ele2 for ele1, ele2 in zip(test_tup1, test_tup2))
# Printing result
print("The modulus tuple : " + str(res))
Output : The original tuple 1 : (10, 4, 5, 6)
The original tuple 2 : (5, 6, 7, 5)
The modulus tuple : (0, 4, 5, 1)
Time complexity: O(n), where n is the length of the tuples.
Auxiliary space: O(n), as a new tuple is created to store the result.
Method #2: Using map() + mod
The combination of the above functionalities can also perform this task. In this, we perform the task of extending logic of modulus using mod and mapping is done by map().
Python3
# Python3 code to demonstrate working of
# Tuple modulo
# using map() + mod
from operator import mod
# initialize tuples
test_tup1 = (10, 4, 5, 6)
test_tup2 = (5, 6, 7, 5)
# printing original tuples
print("The original tuple 1 : " + str(test_tup1))
print("The original tuple 2 : " + str(test_tup2))
# Tuple modulo
# using map() + mod
res = tuple(map(mod, test_tup1, test_tup2))
# printing result
print("The modulus tuple : " + str(res))
Output : The original tuple 1 : (10, 4, 5, 6)
The original tuple 2 : (5, 6, 7, 5)
The modulus tuple : (0, 4, 5, 1)
Time complexity: O(n), where n is the size of the tuples. This is because the program only performs a single pass through the tuples to compute the element-wise modulus using the map() and mod() functions.
Auxiliary space: O(n), where n is the size of the tuples.
Method #3: Using for loop
Python3
# Python3 code to demonstrate working of
# Tuple modulo
# Initialize tuples
test_tup1 = (10, 4, 5, 6)
test_tup2 = (5, 6, 7, 5)
# Printing original tuples
print("The original tuple 1 : " + str(test_tup1))
print("The original tuple 2 : " + str(test_tup2))
# Tuple modulo
res=[]
for i in range(0,len(test_tup1)):
res.append(test_tup1[i]%test_tup2[i])
res=tuple(res)
# Printing result
print("The modulus tuple : " + str(res))
OutputThe original tuple 1 : (10, 4, 5, 6)
The original tuple 2 : (5, 6, 7, 5)
The modulus tuple : (0, 4, 5, 1)
Time complexity: O(n), where n is the length of the tuples.
Auxiliary space: O(n)
Method #4: Using numpy
Note: Install numpy module using the command "pip install numpy"
It provides a convenient way to perform element-wise mathematical operations on arrays and lists.
Python3
import numpy as np
# initialize tuples
test_tup1 = (10, 4, 5, 6)
test_tup2 = (5, 6, 7, 5)
# printing original tuples
print("The original tuple 1 : " + str(test_tup1))
print("The original tuple 2 : " + str(test_tup2))
# Tuple modulo using numpy
res = tuple(np.mod(test_tup1, test_tup2))
# printing result
print("The modulus tuple : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy
Output:
The original tuple 1 : (10, 4, 5, 6)
The original tuple 2 : (5, 6, 7, 5)
The modulus tuple : (0, 4, 5, 1)
Time complexity: O(n) where n is the size of a tuple.
Auxiliary Space: O(n) as we are creating a new tuple of the same size as the original tuple.
Method #5: Using the Recursive method
Algorithm:
- Define a function called modulo_tuple that takes two tuples t1 and t2 as input, along with an optional result tuple which is initially empty.
- Check if the first tuple t1 is empty. If it is, return the result tuple.
- Otherwise, recursively call the modulo_tuple function with the first element of t1 and t2 removed, and the modulo of the first elements of t1 and t2 appended to result.
- Initialize two tuples test_tup1 and test_tup2 with some values.
- Print the original tuples test_tup1 and test_tup2.
- Call the modulo_tuple function with test_tup1 and test_tup2 as arguments and store the result in a variable called result.
- Print the result tuple.
Python3
# Python3 code to demonstrate working of
# Tuple modulo
# using recursive method
def modulo_tuple(t1, t2, result=()):
if not t1:
return result
return modulo_tuple(t1[1:], t2[1:], result + (t1[0] % t2[0],))
# initialize tuples
test_tup1 = (10, 4, 5, 6)
test_tup2 = (5, 6, 7, 5)
# printing original tuples
print("The original tuple 1 : " + str(test_tup1))
print("The original tuple 2 : " + str(test_tup2))
# Tuple modulo
result = modulo_tuple(test_tup1, test_tup2)
# printing result
print("The modulus tuple : " + str(result))
# this code contributed by tvsk
OutputThe original tuple 1 : (10, 4, 5, 6)
The original tuple 2 : (5, 6, 7, 5)
The modulus tuple : (0, 4, 5, 1)
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #6: Using itertools.starmap()
Algorithm:
- Import itertools module.
- Define two tuples test_tup1 and test_tup2.
- Using starmap() function, map the modulus operation with each element of both the tuples using lambda function and zip() function.
- Convert the resultant object into a tuple and store it in the result variable.
- Print the result.
Python3
import itertools
# Initializing list
test_tup1 = (10, 4, 5, 6)
test_tup2 = (5, 6, 7, 5)
# Printing original tuples
print("The original tuple 1 : " + str(test_tup1))
print("The original tuple 2 : " + str(test_tup2))
result = tuple(itertools.starmap(lambda x, y: x %
y, zip(test_tup1, test_tup2)))
# Printing the result
print("The modulus tuple : " + str(result))
# This code is contributed by Jyothi pinjala.
OutputThe original tuple 1 : (10, 4, 5, 6)
The original tuple 2 : (5, 6, 7, 5)
The modulus tuple : (0, 4, 5, 1)
Time complexity: O(n)
Auxiliary Space: O(n)
Method #7: Using heapq
Algorithm:
- Initialize two tuples test_tup1 and test_tup2 with some elements.
- Calculate the modulus of the corresponding elements of the two tuples using zip() and a generator expression.
- Pass the generator expression to heapq.nlargest() with the len(test_tup1) argument to get the modulus of all the pairs of elements.
- Convert the result to a tuple using tuple() and assign it to modulus_tup.
- Print the original tuples and the modulus tuple.
Python3
import heapq
test_tup1 = (10, 4, 5, 6)
test_tup2 = (5, 6, 7, 5)
modulus_tup = tuple(heapq.nlargest(len(test_tup1), (x % y for x, y in zip(test_tup1, test_tup2))))
print("The original tuple 1 : " + str(test_tup1))
print("The original tuple 2 : " + str(test_tup2))
print("The modulus tuple : " + str(modulus_tup))
#This code is contributed by Rayudu.
OutputThe original tuple 1 : (10, 4, 5, 6)
The original tuple 2 : (5, 6, 7, 5)
The modulus tuple : (5, 4, 1, 0)
Time Complexity: O(N)
The time complexity of this code is O(n log n), where n is the length of the input tuples. This is because heapq.nlargest() is used to calculate the modulus tuple, and its time complexity is O(n log k), where k is the length of the heap. In this case, k is equal to the length of the input tuples, so the time complexity is O(n log n).
Auxiliary Space: O(N)
The space complexity of this code is O(n), where n is the length of the input tuples. This is because a generator expression is used to calculate the modulus tuple, and its space complexity is O(1). The result of the generator expression is then converted to a tuple using tuple(), which has a space complexity of O(n). The space complexity of test_tup1, test_tup2, and modulus_tup is also O(n) since they all have n elements.
Method #8: Using List Comprehension
Approach:
- Initialize two tuples: test_tup1 and test_tup2 with integer values.
- Using list comprehension, apply modulus operator to each pair of elements from test_tup1 and test_tup2 tuples.
- Store the result in the res variable.
- Print the original tuples and the modulus tuple.
Python3
# Python3 code to demonstrate working of
# Tuple modulo
# using List comprehension
# initialize tuples
test_tup1 = (10, 4, 5, 6)
test_tup2 = (5, 6, 7, 5)
# printing original tuples
print("The original tuple 1 : " + str(test_tup1))
print("The original tuple 2 : " + str(test_tup2))
# Tuple modulo
# using List comprehension
res = [ele1 % ele2 for ele1, ele2 in zip(test_tup1, test_tup2)]
res = tuple(res)
# printing result
print("The modulus tuple : " + str(res))
OutputThe original tuple 1 : (10, 4, 5, 6)
The original tuple 2 : (5, 6, 7, 5)
The modulus tuple : (0, 4, 5, 1)
Time complexity: O(n), where n is the length of the tuples.
Auxiliary space: O(n), where n is the length of the tuples, because we are storing the result in a list before converting it to a tuple.
Similar Reads
Python - Sum of tuple elements
Sometimes, while programming, we have a problem in which we might need to perform summation among tuple elements. This is an essential utility as we come across summation operations many times and tuples are immutable and hence required to be dealt with. Letâs discuss certain ways in which this task
6 min read
Python - Modulo K elements removal
Due to the upcoming of Machine Learning, focus has now moved on handling the certain values than ever before, the reason behind this is that it is the essential step of data preprocessing before it is fed into further techniques to perform. Hence removal of certain values in essential and knowledge
6 min read
Python - Elements frequency in Tuple
Given a Tuple, find the frequency of each element. Input : test_tup = (4, 5, 4, 5, 6, 6, 5) Output : {4: 2, 5: 3, 6: 2} Explanation : Frequency of 4 is 2 and so on.. Input : test_tup = (4, 5, 4, 5, 6, 6, 6) Output : {4: 2, 5: 2, 6: 3} Explanation : Frequency of 4 is 2 and so on.. Method #1 Using def
7 min read
Python Tuple - len() Method
While working with tuples many times we need to find the length of the tuple, and, instead of counting the number of elements with loops, we can also use Python len(). We will learn about the len() method used for tuples in Python. Example: Python3 Tuple =( 1, 0, 4, 2, 5, 6, 7, 5) print(len(Tuple))
2 min read
Python - Tuple elements inversions
Sometimes, while programming, we have a problem in which we might need to perform certain bitwise operations among tuple elements. This is an essential utility as we come across bitwise operations many times. Letâs discuss certain ways in which this task can be performed. Method #1 : Using map() + l
3 min read
Python | N element incremental tuples
Sometimes, while working with data, we can have a problem in which we require to gather a data that is of the form of sequence of increasing element tuple with each tuple containing the element N times. Let's discuss certain ways in which this task can be performed. Method #1 : Using generator expre
5 min read
Python | Binary Group Tuple list elements
Sometimes, while working with tuples, we can have problems of grouping them, be it based on gender or any particular binary category. This can have applications in many domains. Let's discuss certain ways in which this can be performed. Method #1 : Using generator + loop + zip() The brute force me
4 min read
Python | Elementwise AND in tuples
Sometimes, while working with records, we can have a problem in which we may need to perform mathematical bitwise AND 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
5 min read
Swap tuple elements in list of tuples - Python
The task of swapping tuple elements in a list of tuples in Python involves exchanging the positions of elements within each tuple while maintaining the list structure. Given a list of tuples, the goal is to swap the first and second elements in every tuple. For example, with a = [(3, 4), (6, 5), (7,
3 min read
Python - Add list elements to tuples list
Sometimes, while working with Python tuples, we can have a problem in which we need to add all the elements of a particular list to all tuples of a list. This kind of problem can come in domains such as web development and day-day programming. Let's discuss certain ways in which this task can be don
6 min read