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 list test_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 removal res = [] for val in test_list: if (val % K) : res.append(val) # printing result print (" 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 list test_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 list comprehension # Modulo K elements removal res = [i for i in test_list if (i % K ! = 0 )] # printing result print (" 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]
Method#3: Using Recursive method.
Python3
# Python3 code to demonstrate # Modulo K elements removal # using recursive method # Function to remove elements divisible by K def 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 numbers test_list = [ 1 , 9 , 4 , 7 , 6 , 5 , 8 , 3 ] # Initialize the value of K K = 3 # printing original list print ( "The original list is : " + str (test_list)) # Call the function to remove elements divisible by K res = remove_modulo_k(test_list, K) # printing result print ( "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 list test_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 filter() function #Modulo K elements removal res = list ( filter ( lambda x: x % K ! = 0 , test_list)) #printing result print ( "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 list test_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 enumerate() to remove elements # Modulo K elements removal res = [val for i, val in enumerate (test_list) if val % K ! = 0 ] # printing result print ( "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.
Please Login to comment...