Open In App

Python – Modulo of tuple elements

Last Updated : 08 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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


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)

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:

  1. 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.
  2. Check if the first tuple t1 is empty. If it is, return the result tuple.
  3. 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.
  4. Initialize two tuples test_tup1 and test_tup2 with some values.
  5. Print the original tuples test_tup1 and test_tup2.
  6. Call the modulo_tuple function with test_tup1 and test_tup2 as arguments and store the result in a variable called result.
  7. 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


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)
Auxiliary Space: O(n)

Method #6: Using itertools.starmap()

Algorithm:

  1. Import itertools module.
  2. Define two tuples test_tup1 and test_tup2.
  3. Using starmap() function, map the modulus operation with each element of both the tuples using lambda function and zip() function.
  4. Convert the resultant object into a tuple and store it in the result variable.
  5. 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.


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)
Auxiliary Space: O(n)

Method #7: Using heapq

Algorithm:

  1. Initialize two tuples test_tup1 and test_tup2 with some elements.
  2. Calculate the modulus of the corresponding elements of the two tuples using zip() and a generator expression.
  3. Pass the generator expression to heapq.nlargest() with the len(test_tup1) argument to get the modulus of all the pairs of elements.
  4. Convert the result to a tuple using tuple() and assign it to modulus_tup.
  5. 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.


Output

The 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:

  1. Initialize two tuples: test_tup1 and test_tup2 with integer values.
  2. Using list comprehension, apply modulus operator to each pair of elements from test_tup1 and test_tup2 tuples.
  3. Store the result in the res variable.
  4. 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))


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), 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 - Raise elements of tuple as power to another tuple
Sometimes, while working with records, we can have a problem in which we may need to perform exponentiation, i.e power 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 above functions can be used to perform this
8 min read
Python | Sort tuple list by Nth element of tuple
Sometimes, while working with Python list, we can come across a problem in which we need to sort the list according to any tuple element. These must be a generic way to perform the sort by particular tuple index. This has a good utility in web development domain. Let's discuss certain ways in which this task can be performed. Method #1: Using sort(
8 min read
Python | Replace tuple according to Nth tuple element
Sometimes, while working with data, we might have a problem in which we need to replace the entry in which a particular entry of data is matching. This can be a matching phone no, id etc. This has it's application in web development domain. Let's discuss certain ways in which this task can be performed. Method #1: Using loop + enumerate() This task
8 min read
Python - Convert Tuple String to Integer Tuple
Interconversion of data is a popular problem developer generally deal with. One can face a problem to convert tuple string to integer tuple. Let's discuss certain ways in which this task can be performed. Method #1 : Using tuple() + int() + replace() + split() The combination of above methods can be used to perform this task. In this, we perform th
7 min read
Python - Convert Tuple to Tuple Pair
Sometimes, while working with Python Tuple records, we can have a problem in which we need to convert Single tuple with 3 elements to pair of dual tuple. This is quite a peculiar problem but can have problems in day-day programming and competitive programming. Let's discuss certain ways in which this task can be performed. Input : test_tuple = ('A'
10 min read
Python - Flatten tuple of List to tuple
Sometimes, while working with Python Tuples, we can have a problem in which we need to perform the flattening of tuples, which have listed as their constituent elements. This kind of problem is common in data domains such as Machine Learning. Let's discuss certain ways in which this task can be performed. Input : test_tuple = ([5], [6], [3], [8]) O
7 min read
Python Program to Convert Tuple Matrix to Tuple List
Given a Tuple Matrix, flatten to tuple list with each tuple representing each column. Example: Input : test_list = [[(4, 5), (7, 8)], [(10, 13), (18, 17)]] Output : [(4, 7, 10, 18), (5, 8, 13, 17)] Explanation : All column number elements contained together. Input : test_list = [[(4, 5)], [(10, 13)]] Output : [(4, 10), (5, 13)] Explanation : All co
8 min read
Python program to convert Set into Tuple and Tuple into Set
Let's see how to convert the set into tuple and tuple into the set. For performing the task we are use some methods like tuple(), set(), type(). tuple(): tuple method is used to convert into a tuple. This method accepts other type values as an argument and returns a tuple type value.set(): set method is to convert other type values to set this meth
7 min read
Python Program to find tuple indices from other tuple list
Given Tuples list and search list consisting of tuples to search, our task is to write a Python Program to extract indices of matching tuples. Input : test_list = [(4, 5), (7, 6), (1, 0), (3, 4)], search_tup = [(3, 4), (8, 9), (7, 6), (1, 2)]Output : [3, 1]Explanation : (3, 4) from search list is found on 3rd index on test_list, hence included in r
8 min read
Python Program to Merge tuple list by overlapping mid tuple
Given two lists that contain tuples as elements, the task is to write a Python program to accommodate tuples from the second list between consecutive tuples from the first list, after considering ranges present between both the consecutive tuples from the first list. Input : test_list1 = [(4, 8), (19, 22), (28, 30), (31, 50)], test_list2 = [(10, 12
11 min read
Practice Tags :
three90RightbarBannerImg