Python | Every Kth element in list

Sometimes, while working with Python lists, we can have a problem in which we require to extract every Kth element of list and slice out a new list from that. This type of problems are quite common as a variation of list slicing. Let’s discuss a way in which this can be done.

Method : Using list slicing
This task can be performed using list slicing functionality. The trick here is to use the skip functionality of list to get every Kth element of list. K, as defined can be used as skip element.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# Kth element list
# Using list slicing 
  
# initializing list
test_list = [6, 4, 8, 9, 10, 5, 8, 9, 10, 2, 34, 5]
  
# printing list
print("The original list : " + str(test_list))
  
# initializing K 
K = 3
  
# Kth element list
# Using list slicing 
res = test_list[::K]
  
# Printing result
print("Kth element list is : " + str(res))

chevron_right


Output :

 
The original list : [6, 4, 8, 9, 10, 5, 8, 9, 10, 2, 34, 5]
Kth element list is : [6, 9, 8, 2]


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.