Python | Every Kth element in list
Last Updated :
12 Apr, 2023
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.
Python3
# 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))
Output :
The original list : [6, 4, 8, 9, 10, 5, 8, 9, 10, 2, 34, 5]
Kth element list is : [6, 9, 8, 2]
Time Complexity: O(n) where n is the number of elements in the list “test_list”. list slicing performs n number of operations.
Auxiliary Space: O(n), extra space is required where n is the number of elements in the list
Method : Using itertools
Another approach could be using itertools.islice() method which can take the start position and the step size to extract every Kth element and return it as an iterator, this approach would have a time complexity of O(n) where n is the number of elements in the list, and a space complexity of O(n/k) as it requires a new list to be created.
Python3
import itertools
# initializing list
test_list = [6, 4, 8, 9, 10, 5, 8, 9, 10, 2, 34, 5]
# printing original list
print("The original list : " + str(test_list))
# initializing K
K = 3
# Kth element list
# Using itertools.islice()
res = list(itertools.islice(test_list, 0, len(test_list), K))
# Printing result
print("Kth element list is : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy
OutputThe original list : [6, 4, 8, 9, 10, 5, 8, 9, 10, 2, 34, 5]
Kth element list is : [6, 9, 8, 2]
This code uses the itertools.islice() method to extract every Kth element in the input list and return it as an iterator. Then it converts the iterator to a list and print the result. The parameters passed to the islice method are (iterable, start, stop, step), where iterable is the list, start is the starting index, stop is the ending index, and step is the number of elements to skip before returning the next element. In this case, we start from the first element (index 0), end at the last element of the list, and skip K-1 elements in between. This allows us to extract every Kth element in the list.
Similar Reads
Python - Find Kth Even Element Given a List, extract Kth occurrence of Even Element. Input : test_list = [4, 6, 2, 3, 8, 9, 10, 11], K = 3 Output : 8 Explanation : K = 3, i.e 0 based index, 4, 6, 2 and 4th is 8. Input : test_list = [4, 6, 2, 3, 8, 9, 10, 11], K = 2 Output : 2 Explanation : K = 2, i.e 0 based index, 4, 6, and 3rd
5 min read
Python | Operate on every Kth element of list We generally wish to employ a particular function to all the elements in a list. But sometimes, according to the requirement, we would wish to employ a particular functionality to certain elements of the list, basically to every Kth element in list. Let's discuss certain ways in which this can be pe
5 min read
Insert after every Nth element in a list - Python Inserting an element after every Nth item in a list is a useful way to adjust the structure of a list. For Example we have a list li=[1,2,3,4,5,6,7]Let's suppose we want to insert element 'x' after every 2 elements in the list so the list will look like li=[1,2,'x',3,4,'x',5,6,7] Iteration with inde
4 min read
Python - Extract Kth element of every Nth tuple in List Given list of tuples, extract Kth column element of every Nth tuple. Input :test_list = [(4, 5, 3), (3, 4, 7), (4, 3, 2), (4, 7, 8), (6, 4, 7), (2, 5, 7), (1, 9, 10), (3, 5, 7)], K = 2, N = 3 Output : [3, 8, 10] Explanation : From 0th, 3rd, and 6th tuple, 2nd elements are 3, 8, 10. Input :test_list
8 min read
Python - Multiply K to every Nth element We generally wish to employ a particular function to all the elements in a list. But sometimes, according to requirement we would wish to employ a particular functionality to certain elements of the list, basically to every Nth element in list. Letâs discuss certain ways in which this can be perform
5 min read
Python | Get Last N Elements from Given List This article discusses ways to fetch the last N elements of a list in Python.Example: Using list SlicingPythontest_list = [4, 5, 2, 6, 7, 8, 10] # initializing N N = 5 # using list slicing res = test_list[-N:] print(str(res)) Output[2, 6, 7, 8, 10] Explaination: This problem can be performed in 1 li
3 min read