Python | Every Kth element removal in List

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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]

 

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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]



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.