Open In App

Python | Modulo K List

Last Updated : 09 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

While working with the python lists, we can come over a situation in which we require to modulo the integer k to each element in the list. We possibly need to iterate and modulo k to each element but that would increase the line of code. Let’s discuss certain shorthands to perform this task. 

Method #1 : Using List Comprehension List comprehension is just the short way to perform the task we perform using the naive method. This is mainly useful to save time and also is best among others when it comes to readability of the code. 

Python3




# Python3 code to demonstrate
# Modulo K List
# using list comprehension
 
# initializing list
test_list = [4, 5, 6, 3, 9]
 
# printing original list
print ("The original list is : " + str(test_list))
 
# initializing K
K = 4
 
# using list comprehension
# Modulo K List
res = [x % K for x in test_list]
 
# printing result
print ("The list after Modulo K to each element : " + str(res))


Output : 

The original list is : [4, 5, 6, 3, 9]
The list after Modulo K to each element : [0, 1, 2, 3, 1]

Time Complexity: O(n), where n is the length of the list test_list 
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list 

  Method #2 : Using map() + lambda The map function can be used to pair each element with the lambda function which performs the task of modulo K to each element in the list. 

Python3




# Python3 code to demonstrate
# Modulo K List
# using map() + lambda
 
# initializing list
test_list = [4, 5, 6, 3, 9]
 
# printing original list
print ("The original list is : " + str(test_list))
 
# initializing K
K = 4
 
# using map() + lambda
# Modulo K List
res = list(map(lambda x : x % K, test_list))
 
# printing result
print ("The list after Modulo K to each element : " + str(res))


Output : 

The original list is : [4, 5, 6, 3, 9]
The list after Modulo K to each element : [0, 1, 2, 3, 1]

Time Complexity: O(n), where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n), where n is the number of elements in the list “test_list”.

Method#3:Using itertools module

Approach

this approach uses the itertools module to generate a list of K values that repeats itself, and then calculates the modulo of each element in the original list with the corresponding value from the generated list. The islice function is used to ensure that we only generate as many values as needed to match the length of the original list, which helps to conserve memory.

Algorithm

1. Import the itertools module
2. Take input the original list and K
3. Use the cycle function from itertools to repeat the values in the range from 0 to K-1
4. Use the islice function from itertools to get the first N values from the cycle, where N is the length of the original list
5. Convert the result of islice to a list and return

Python3




from itertools import cycle, islice
 
def modulo_k_list(arr, k):
    cycle_range = cycle(range(k))
    result = list(islice(cycle_range, len(arr)))
    return [(arr[i] % k) for i in range(len(arr))]
 
arr = [4, 5, 6, 3, 9]
k=4
print(modulo_k_list(arr, k))


Output

[0, 1, 2, 3, 1]

Time Complexity: O(n), where n is the length of the input list.
Space Complexity: O(n), where n is the length of the input list.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads