Thursday, December 26, 2024
Google search engine
HomeLanguagesPython – Lists Modulo

Python – Lists Modulo

Sometimes we come across the situations in which we require to apply a particular function to each elements of two lists at similar index. These are quite similar and come up as application for certain utilities. Let’s discuss certain ways in which the modulo, i.e remainder of two lists can be performed. 

Method #1: Using zip() + list comprehension The zip operation can be used to link one list with the other and the computation part can be handled by the list comprehension and hence providing a shorthand to this particular problem. 

Python3




# Python3 code to demonstrate
# Lists Modulo
# using zip() + list comprehension
 
# initializing lists
test_list1 = [3, 5, 2, 6, 4]
test_list2 = [7, 3, 4, 1, 5]
 
# printing original lists
print ("The original list 1 is : " + str(test_list1))
print ("The original list 2 is : " + str(test_list2))
 
# Lists Modulo
# using zip() + list comprehension
res = [i % j for i, j in zip(test_list1, test_list2)]
 
# printing result
print ("The modulo list is : " + str(res))


Output

The original list 1 is : [3, 5, 2, 6, 4]
The original list 2 is : [7, 3, 4, 1, 5]
The modulo list is : [3, 2, 2, 0, 4]

Time complexity: O(M^N) as the number of combinations generated is M choose N.
Auxiliary space: O(M^N) as the size of the resultant list is also M choose N.

  Method #2 : Using map() Using map function is most elegant way in which we can possibly perform the twining of a function with both the lists. Different operations other than modulo can also be applied over it. 

Python3




# Python3 code to demonstrate
# Lists Modulo
# using map()
from operator import mod
 
# initializing lists
test_list1 = [3, 5, 2, 6, 4]
test_list2 = [7, 3, 4, 1, 5]
 
# printing original lists
print ("The original list 1 is : " + str(test_list1))
print ("The original list 2 is : " + str(test_list2))
 
# Lists Modulo
# using map()
res = list(map(mod, test_list1, test_list2))
 
# printing result
print ("The modulo list is : " + str(res))


Output

The original list 1 is : [3, 5, 2, 6, 4]
The original list 2 is : [7, 3, 4, 1, 5]
The modulo list is : [3, 2, 2, 0, 4]

Time complexity: O(M^N) as the number of combinations generated is M choose N.
Auxiliary space: O(M^N) as the size of the resultant list is also M choose N.

Method #3 : Using numpy() 

Note: Install numpy module using command “pip install numpy”

Python3




import numpy as np
 
test_list1 = [3, 5, 2, 6, 4]
test_list2 = [7, 3, 4, 1, 5]
 
# Using numpy mod function to perform modulo on the two lists
res = np.mod(test_list1, test_list2)
 
# print result
print("The modulo list is : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy


output:

The modulo list is : [3 2 2 0 4]

This approach uses the mod() function from the numpy library to perform the modulo operation on elements from two lists element-wise. The mod() function accepts two lists as input and returns a new list with the result of the modulo operation on each pair of elements at the same index.

Time complexity: O(n)
Auxiliary Space: O(n)

Note that this approach requires the numpy library to be installed.

Method #4 : Using a for loop:

Python3




test_list1 = [3, 5, 2, 6, 4]
test_list2 = [7, 3, 4, 1, 5]
res = []
# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
for i in range(len(test_list1)):
    res.append(test_list1[i] % test_list2[i])
print("The modulo list is: ", res)


Output

The original list 1 is : [3, 5, 2, 6, 4]
The original list 2 is : [7, 3, 4, 1, 5]
The modulo list is:  [3, 2, 2, 0, 4]

Time complexity: O(n)
Auxiliary Space: O(n)

RELATED ARTICLES

Most Popular

Recent Comments