Open In App

Python – Modulo K elements removal

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

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))


Output : 

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))


Output : 

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 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


Output

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))


Output

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:

  1. Initialize a list of integers test_list.
  2. Print the original list.
  3. Initialize a variable K to the value 3.
  4. Use a list comprehension and the enumerate() function to loop through each element val and its corresponding index i in test_list.
  5. Check if val % K != 0 to see if val is not a multiple of K.
  6. If val is not a multiple of K, add it to the new list res.
  7. 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.


Output

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.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads