Open In App

Python | Every Kth element removal in List

Last Updated : 07 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

We generally wish to employ a particular function to all the elements in a list. But sometimes, according to requirement we would wish to remove certain elements of the list, basically to every Kth element in list. Let’s discuss certain ways in which this can be performed. 

Method #1 : Using list comprehension + enumerate() The functionality of getting every Kth number of list can be done with the help of list comprehension and enumerate function helps in the iteration of the whole list. 

Python3




# Python3 code to demonstrate
# Every Kth element removal in List
# using list comprehension + enumerate()
 
# initializing list
test_list = [1, 4, 5, 6, 7, 8, 9, 12]
 
# printing the original list
print ("The original list is : " + str(test_list))
 
# initializing K
K = 3
 
# using list comprehension + enumerate()
# Every Kth element removal in List
# Remove every third element
res = [i for j, i in enumerate(test_list) if j % K != 0]
 
# printing result
print ("The list after removing every Kth element : " + str(res))


Output : 

The original list is : [1, 4, 5, 6, 7, 8, 9, 12]
The list after removing every kth element : [4, 5, 7, 8, 12]

Time Complexity: O(n*n) where n is the number of elements in the list “test_list”. The list comprehension + enumerate() is used to perform the task and it takes O(n*n) time.
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the list “test_list”.

  Method #2 : Using list comprehension + list slicing Above mentioned functions can help to perform these tasks. The list comprehension does the task of iteration in list and list slicing does the extraction of every Kth element. 

Python3




# Python3 code to demonstrate
# Every Kth element removal in List
# using list comprehension + list slicing
 
# initializing list
test_list = [1, 4, 5, 6, 7, 8, 9, 12]
 
# printing the original list
print ("The original list is : " + str(test_list))
 
# initializing K
K = 3
 
# using list comprehension + list slicing
# Every Kth element removal in List
# removes every 3rd element
res = [i for i in test_list if i not in test_list[0 :: 3]]
 
# printing result
print ("The list after removing every Kth element : " + str(res))


Output : 

The original list is : [1, 4, 5, 6, 7, 8, 9, 12]
The list after removing every kth element : [4, 5, 7, 8, 12]

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

Python3




#Python3 code to demonstrate
#Every Kth element removal in List
#using filter()
#initializing list
test_list = [1, 4, 5, 6, 7, 8, 9, 12]
 
#printing the original list
print ("The original list is : " + str(test_list))
 
#initializing K
K = 3
 
#using filter()
#Every Kth element removal in List
#removes every 3rd element
res = list(filter(lambda x: test_list.index(x) % K != 0, test_list))
 
#printing result
print ("The list after removing every Kth element : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy


Output

The original list is : [1, 4, 5, 6, 7, 8, 9, 12]
The list after removing every Kth element : [4, 5, 7, 8, 12]

Time Complexity: O(n)
Auxiliary Space: O(n)
Explanation:
In this approach, we use the filter() function to filter out every Kth element from the list. The lambda function takes the current element and checks its index in the original list. If the index is not divisible by K, it returns True and the element is included in the result, otherwise it returns False and the element is excluded from the result.

Method#4: Using for loop

 here’s the step-by-step algorithm for removing every Kth element from a given list:

  1. Initialize an empty list new_list.
  2. Loop through each element in the original list using the range() function and the len() function.
  3. Check if the index of the current element i is a multiple of K using the modulo operator %. If it is, skip adding this element to the new list.
  4. If the index is not a multiple of K, add the current element to new_list.
  5. After the loop, return new_list.

Python3




test_list = [1, 4, 5, 6, 7, 8, 9, 12]
#printing the original list
print ("The original list is : " + str(test_list))
K = 3
 
new_list = []
for i in range(len(test_list)):
    if (i) % K != 0:
        new_list.append(test_list[i])
 
print("The list after removing every Kth element: ", new_list)
#This code is contributed by Vinay pinjala.


Output

The original list is : [1, 4, 5, 6, 7, 8, 9, 12]
The list after removing every Kth element:  [4, 5, 7, 8, 12]

The time complexity of this algorithm is O(n), where n is the length of the input list. This is because we iterate through every element in the list once and perform a constant amount of work for each element.

The auxiliary space of this algorithm is also O(n), where n is the length of the input list. This is because we create a new list new_list that may have up to n elements, depending on how many elements are removed from the original list. 



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

Similar Reads