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 of it is a must. Lets discuss certain ways in which removal of modulo K values is achieved.
Method #1 : Naive Method In naive method, we iterate through whole list and append all the filtered, non modulo K values into a new list, hence ready to be performed with subsequent operations.
Python3
# Python3 code to demonstrate # Modulo K elements removal# using naive method # initializing listtest_list = [1, 9, 4, 7, 6, 5, 8, 3]# printing original list print ("The original list is : " + str(test_list))# initializing K K = 3# using naive method # Modulo K elements removalres = []for val in test_list: if (val % K) : res.append(val)# printing resultprint ("List after removal of modulo K values : " + str(res)) |
The original list is : [1, 9, 4, 7, 6, 5, 8, 3] List after removal of modulo K values : [1, 4, 7, 5, 8]
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 #2 : Using list comprehension The longer task of using the naive method and increasing line of codes can be done in compact way using this method. We just check for non modulo K values and construct the new filtered list.
Python3
# Python3 code to demonstrate # Modulo K elements removal# using list comprehension# initializing listtest_list = [1, 9, 4, 7, 6, 5, 8, 3]# printing original list print ("The original list is : " + str(test_list))# initializing KK = 3# using list comprehension# Modulo K elements removalres = [i for i in test_list if(i % K != 0)]# printing resultprint ("List after removal of modulo K values : " + str(res)) |
The original list is : [1, 9, 4, 7, 6, 5, 8, 3] List after removal of modulo K values : [1, 4, 7, 5, 8]
Time Complexity: O(n) where n is the number of elements in the test_list. The list comprehension is used to perform the task and it takes O(n) time.
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the test list.
Method#3: Using Recursive method.
Python3
# Python3 code to demonstrate# Modulo K elements removal# using recursive method# Function to remove elements divisible by Kdef remove_modulo_k(test_list, K): # Base case: if the list is empty, return an empty list if not test_list: return [] # Recursive case: remove the first element if it's divisible by K # and call the function on the rest of the list if test_list[0] % K == 0: return remove_modulo_k(test_list[1:], K) else: # Return the first element concatenated with the result of the function call return [test_list[0]] + remove_modulo_k(test_list[1:], K)# Initialize the list of numberstest_list = [1, 9, 4, 7, 6, 5, 8, 3]# Initialize the value of KK = 3# printing original listprint ("The original list is : " + str(test_list))# Call the function to remove elements divisible by Kres = remove_modulo_k(test_list, K)# printing resultprint ("List after removal of modulo K values : " + str(res))#this code contributed by tvsk |
The original list is : [1, 9, 4, 7, 6, 5, 8, 3] List after removal of modulo K values : [1, 4, 7, 5, 8]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method#4: Here is another approach using filter() function in python:
Python3
#Python3 code to demonstrate#Modulo K elements removal#using filter() function#initializing listtest_list = [1, 9, 4, 7, 6, 5, 8, 3]#printing original listprint("The original list is : " + str(test_list))#initializing KK = 3#using filter() function#Modulo K elements removalres = list(filter(lambda x: x % K != 0, test_list))#printing resultprint("List after removal of modulo K values : " + str(res)) |
The original list is : [1, 9, 4, 7, 6, 5, 8, 3] List after removal of modulo K values : [1, 4, 7, 5, 8]
Time Complexity: O(n)
Auxiliary Space: O(n)
In this approach, the filter() function takes two arguments, first one is the lambda function that returns True if the element is not divisible by K, and second one is the list of elements. The filter() function returns an iterator and to get the result as a list, we need to convert it to a list.
Method#5:Using enumerate
The given code removes elements from a list that are multiples of K, using list comprehension and the enumerate() function.
Here’s a step-by-step explanation of the algorithm:
- Initialize a list of integers test_list.
- Print the original list.
- Initialize a variable K to the value 3.
- Use a list comprehension and the enumerate() function to loop through each element val and its corresponding index i in test_list.
- Check if val % K != 0 to see if val is not a multiple of K.
- If val is not a multiple of K, add it to the new list res.
- After the loop, return res.
Python3
# Python3 code to demonstrate# Modulo K elements removal# using enumerate()# initializing listtest_list = [1, 9, 4, 7, 6, 5, 8, 3]# printing original listprint("The original list is : " + str(test_list))# initializing KK = 3# using enumerate() to remove elements# Modulo K elements removalres = [val for i, val in enumerate(test_list) if val % K != 0]# printing resultprint("List after removal of modulo K values : " + str(res))#This code is contributed by Vinay Pinjala. |
The original list is : [1, 9, 4, 7, 6, 5, 8, 3] List after removal of modulo K values : [1, 4, 7, 5, 8]
The time complexity of this algorithm is O(n), where n is the length of the list test_list. This is because we loop through each element in test_list and perform a constant-time check to see if it is a multiple of K.
The auxiliary space of this algorithm is O(n), where n is the length of the list test_list. This is because we create a new list res to store the remaining elements. The memory usage grows linearly with the size of the input list.



