Python | Nested Records Modulo
Last Updated :
27 Apr, 2023
Sometimes, while working with records, we can have a problem in which we require to perform index wise remainder of tuple elements. This can get complicated with tuple elements to be tuple and inner elements again be tuple. Let’s discuss certain ways in which this problem can be solved.
Method #1 : Using zip() + nested generator expression The combination of above functions can be used to perform the task. In this, we combine the elements across tuples using zip(). The iterations and modulo logic is provided by generator expression.
Python3
# Python3 code to demonstrate working of
# Nested Records Modulo
# using zip() + nested generator expression
# initialize tuples
test_tup1 = ((1, 3), (4, 5), (2, 9), (1, 10))
test_tup2 = ((6, 7), (3, 9), (1, 1), (7, 3))
# printing original tuples
print("The original tuple 1 : " + str(test_tup1))
print("The original tuple 2 : " + str(test_tup2))
# Nested Records Modulo
# using zip() + nested generator expression
res = tuple(tuple(a % b for a, b in zip(tup1, tup2))\
for tup1, tup2 in zip(test_tup1, test_tup2))
# printing result
print("The resultant tuple after modulo : " + str(res))
OutputThe original tuple 1 : ((1, 3), (4, 5), (2, 9), (1, 10))
The original tuple 2 : ((6, 7), (3, 9), (1, 1), (7, 3))
The resultant tuple after modulo : ((1, 3), (1, 5), (0, 0), (1, 1))
Time Complexity: O(n*n), where n is the number of elements in the list “test_tup”.
Auxiliary Space: O(n*n), where n is the number of elements in the list “test_tup”.
Method #2 : Using isinstance() + zip() + loop + list comprehension The combination of above functions can be used to perform this particular task. In this, we check for the nesting type and perform recursion. This method can give flexibility of more than 1 level nesting.
Python3
# Python3 code to demonstrate working of
# Nested Records Modulo
# using isinstance() + zip() + loop + list comprehension
# function to perform task
def tup_mod(tup1, tup2):
if isinstance(tup1, (list, tuple)) and isinstance(tup2, (list, tuple)):
return tuple(tup_mod(x, y) for x, y in zip(tup1, tup2))
return tup1 % tup2
# initialize tuples
test_tup1 = ((1, 3), (4, 5), (2, 9), (1, 10))
test_tup2 = ((6, 7), (3, 9), (1, 1), (7, 3))
# printing original tuples
print("The original tuple 1 : " + str(test_tup1))
print("The original tuple 2 : " + str(test_tup2))
# Nested Records Modulo
# using isinstance() + zip() + loop + list comprehension
res = tuple(tup_mod(x, y) for x, y in zip(test_tup1, test_tup2))
# printing result
print("The resultant tuple after modulo : " + str(res))
OutputThe original tuple 1 : ((1, 3), (4, 5), (2, 9), (1, 10))
The original tuple 2 : ((6, 7), (3, 9), (1, 1), (7, 3))
The resultant tuple after modulo : ((1, 3), (1, 5), (0, 0), (1, 1))
Time Complexity: O(n*n), where n is the length of the list test_tup
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list
Method #3 : Using numpy
Note: Install numpy module using command "pip install numpy"
Python3
import numpy as np
# Initialize the nested tuples
test_tup1 = ((1, 3), (4, 5), (2, 9), (1, 10))
test_tup2 = ((6, 7), (3, 9), (1, 1), (7, 3))
# Convert the nested tuples to numpy arrays
arr1 = np.array(test_tup1)
arr2 = np.array(test_tup2)
# Perform modulo operation
result = np.mod(arr1, arr2)
# Convert the result back to nested tuples
result = result.tolist()
result = [tuple(i) for i in result]
result = tuple(result)
print("The resultant tuple after modulo:", result)
#This code is contributed by Edula Vinay Kumar Reddy
Output:
The resultant tuple after modulo: ((1, 3), (1, 5), (0, 0), (1, 1))
Explanation:
We start by importing Numpy library.
Then we initialize two nested tuples test_tup1 and test_tup2.
Next, we convert these nested tuples to Numpy arrays using np.array method.
Then we perform modulo operation on the arrays using np.mod method.
Finally, we convert the result back to nested tuples using the tolist method and list comprehension.
Method #4 : Using for loop + tuple() method
Python3
# Python3 code to demonstrate working of
# Nested Records Modulo
# initialize tuples
test_tup1 = ((1, 3), (4, 5), (2, 9), (1, 10))
test_tup2 = ((6, 7), (3, 9), (1, 1), (7, 3))
# printing original tuples
print("The original tuple 1 : " + str(test_tup1))
print("The original tuple 2 : " + str(test_tup2))
# Nested Records Modulo
res=[]
for i in range(0,len(test_tup1)):
x=[]
a=test_tup1[i][0]%test_tup2[i][0]
b=test_tup1[i][1]%test_tup2[i][1]
x.append(a)
x.append(b)
res.append(tuple(x))
# printing result
print("The resultant tuple after modulo : " + str(res))
OutputThe original tuple 1 : ((1, 3), (4, 5), (2, 9), (1, 10))
The original tuple 2 : ((6, 7), (3, 9), (1, 1), (7, 3))
The resultant tuple after modulo : [(1, 3), (1, 5), (0, 0), (1, 1)]
Time Complexity : O(N)
Auxiliary Space : O(N)
Method #5 : Using map() and lambda functions:
Python3
# Python3 code to demonstrate working of
# Nested Records Modulo
# using map() + lambda
# initialize tuples
test_tup1 = ((1, 3), (4, 5), (2, 9), (1, 10))
test_tup2 = ((6, 7), (3, 9), (1, 1), (7, 3))
# printing original tuples
print("The original tuple 1 : " + str(test_tup1))
print("The original tuple 2 : " + str(test_tup2))
# Nested Records Modulo
# using map() + lambda
res = tuple(map(lambda x, y: tuple(map(lambda a, b: a % b, x, y)), test_tup1, test_tup2))
# printing result
print("The resultant tuple after modulo : " + str(res))
#This code is contributed by Jyothi pinjala
OutputThe original tuple 1 : ((1, 3), (4, 5), (2, 9), (1, 10))
The original tuple 2 : ((6, 7), (3, 9), (1, 1), (7, 3))
The resultant tuple after modulo : ((1, 3), (1, 5), (0, 0), (1, 1))
Time Complexity : O(N)
Auxiliary Space : O(N)
Similar Reads
Python - Nested Records List from Lists
Sometimes, while working with Python Data, we can have problems in which we have data incoming in different formats. In this, we can receive data as key and value in separate dictionaries and we are required to make values as list of records with a new key. Let's discuss certain ways in which we can
6 min read
Python | Records list XOR
Sometimes, while programming, we have a problem in which we might need to perform certain bitwise operations among tuple list elements. This is an essential utility as we come across bitwise operations many times. Letâs discuss certain ways in which XOR can be performed. Method #1 : Using reduce() +
5 min read
Python - Remove nested records from tuple
Sometimes, while working with records, we can have a problem in which an element of a record is another tuple records and we might have to remove the nested records. This is a problem which does not occur commonly, but having a solution to it is useful. Letâs discuss certain way in which this task c
5 min read
Python - Remove None Nested Records
Sometimes, while working with Python Records, can have problem in which we need to perform the removal of data which have all key's values as None in nested records. This kind of problem can have application in data preprocessing. Lets discuss certain ways in which this task can be performed. Method
4 min read
Python | Records Intersection
Sometimes, while working with tuples, we can have a problem in which we need similar features of two records. This type of application can come in the Data Science domain. Letâs discuss certain ways in which this problem can be solved. Method #1: Using set() + "&" operator This task can be perfo
6 min read
Python - Cumulative Records Product
Sometimes, while working with data in form of records, we can have a problem in which we need to find the product element of all the records received. This is a very common application that can occur in Data Science domain. Letâs discuss certain ways in which this task can be performed. Method #1 :
5 min read
Python | Exponentiate Kth Record 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 exponentiated to Kth element of tuple in list. Method #1 : Using loop Using loops thi
5 min read
Python | Difference in Record Lists
Sometimes, while working with data, we may have a problem in which we require to find the difference records between two lists that we receive. This is a very common problem and records usually occurs as a tuple. Letâs discuss certain ways in which this problem can be solved. Method #1 : Using list
5 min read
Python - Values from custom List in Records
Sometimes, while working with Python records, we can have a problem in which we need to extract all the values from the custom list in list dictionary records. This problem can have applications in domains such as web development and school programming. Let's discuss certain ways in which this task
7 min read
Python - Occurrence counter in List of Records
Sometimes, while dealing with records we can have a problem in which we need count the occurrence of incoming digits corresponding to different characters/players in a game and compile them in a dictionary. This can have application in gaming and web development. Lets discuss a way in which this can
2 min read