Python – Modulo K elements removal
Last Updated :
13 Apr, 2023
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
test_list = [ 1 , 9 , 4 , 7 , 6 , 5 , 8 , 3 ]
print ("The original list is : " + str (test_list))
K = 3
res = []
for val in test_list:
if (val % K) :
res.append(val)
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
test_list = [ 1 , 9 , 4 , 7 , 6 , 5 , 8 , 3 ]
print ("The original list is : " + str (test_list))
K = 3
res = [i for i in test_list if (i % K ! = 0 )]
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
def remove_modulo_k(test_list, K):
if not test_list:
return []
if test_list[ 0 ] % K = = 0 :
return remove_modulo_k(test_list[ 1 :], K)
else :
return [test_list[ 0 ]] + remove_modulo_k(test_list[ 1 :], K)
test_list = [ 1 , 9 , 4 , 7 , 6 , 5 , 8 , 3 ]
K = 3
print ( "The original list is : " + str (test_list))
res = remove_modulo_k(test_list, K)
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)
Method#4: Here is another approach using filter() function in python:
Python3
test_list = [ 1 , 9 , 4 , 7 , 6 , 5 , 8 , 3 ]
print ( "The original list is : " + str (test_list))
K = 3
res = list ( filter ( lambda x: x % K ! = 0 , test_list))
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:
- 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
test_list = [ 1 , 9 , 4 , 7 , 6 , 5 , 8 , 3 ]
print ( "The original list is : " + str (test_list))
K = 3
res = [val for i, val in enumerate (test_list) if val % K ! = 0 ]
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]
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.
Similar Reads
Python - Odd elements removal in List
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
5 min read
Python - Modulo of tuple elements
Sometimes, while working with records, we can have a problem in which we may need to perform a modulo of tuples. This problem can occur in day-day programming. Letâs discuss certain ways in which this task can be performed. Method #1: Using zip() + generator expression The combination of the above f
8 min read
Remove last K elements of list - Python
Given a list and an integer K, the task is to remove the last K elements from the list. For example, if the list is [1, 2, 3, 4, 5] and K = 2, the result should be [1, 2, 3]. Letâs explore different methods to remove the last K elements from a list in Python. Using list slicingList slicing is one of
3 min read
Python - Rear Kth elements
Given a list, the task is to extract all Kth elements from rear end. Input : test_list = [3, 4, 5, 2, 1], K = 2 Output : [1, 5, 3] Explanation : Every 2nd elements are extracted from rear starting from 1. Input : test_list = [3, 4, 5], K = 1 Output : [5, 4, 3] Explanation : Every elements are extrac
3 min read
Python - K middle elements
Given a List, extract K elements occurring in middle of list. Input : test_list = [2, 3, 5, 7, 8, 5, 3, 5, 9], K = 3 Output : [7, 8, 5] Explanation : Middle 3 elements are extracted. Input : test_list = [2, 3, 5, 7, 8, 5, 3, 5, 9], K = 7 Output : [3, 5, 7, 8, 5, 3, 5] Explanation : Middle 7 elements
5 min read
Python | Remove Front K elements
We often come to situations in which we need to decrease the size of the list by truncating the first elements of the list. This particular problem occurs when we need to optimize memory. This has its application in the day-day programming when sometimes we require to get all the lists of similar si
7 min read
Python - Remove multiple elements from Set
Given a set, the task is to write a Python program remove multiple elements from set. Example: Input : test_set = {6, 4, 2, 7, 9}, rem_ele = [2, 4, 8] Output : {9, 6, 7} Explanation : 2, 4 are removed from set. Input : test_set = {6, 4, 2, 7, 9}, rem_ele = [4, 8] Output : {2, 9, 6, 7} Explanation :
4 min read
Python - K elements Reversed Slice
Given List of elements, perform K elements reverse slice. Input : test_list = [2, 4, 6, 8, 3, 9, 12, 15, 16, 18], K = 3 Output : [18, 16, 15] Explanation : 3 elements sliced from rear end. Input : test_list = [2, 4, 6, 8], K = 3 Output : [8, 6, 4] Explanation : 3 elements sliced from rear end, 8, 6
4 min read
Remove Multiple Elements from List in Python
In this article, we will explore various methods to remove multiple elements from a list in Python. The simplest way to do this is by using a loop. A simple for loop can also be used to remove multiple elements from a list. [GFGTABS] Python a = [10, 20, 30, 40, 50, 60, 70] # Elements to remove remov
3 min read
Python - Remove elements at Indices in List
Given List, remove all the elements present in the indices list in Python. Input : test_list = [5, 6, 3, 7, 8, 1, 2, 10], idx_list = [2, 4, 5] Output : [5, 6, 7, 2, 10] Explanation : 3, 6, and 1 has been removed. Input : test_list = [5, 6, 3, 7, 8, 1, 2, 10], idx_list = [2] Output : [5, 6, 7, 8, 1,
7 min read