Python | Operate on every Kth element of list
Last Updated :
25 Apr, 2023
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 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
test_list = [ 1 , 4 , 5 , 6 , 7 , 8 , 9 , 12 ]
print ( "The original list is : " + str (test_list))
res = [i + 2 if j % 3 = = 0 else i
for j, i in enumerate (test_list)]
print ( "The list after editing every kth element : " + str (res))
|
Output
The original list is : [1, 4, 5, 6, 7, 8, 9, 12]
The list after editing every kth element : [3, 4, 5, 8, 7, 8, 11, 12]
Time Complexity: O(n), where n is the length of the list test_list
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res 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
test_list = [ 1 , 4 , 5 , 6 , 7 , 8 , 9 , 12 ]
print ("The original list is : " + str (test_list))
test_list[ 0 :: 3 ] = [i + 2 for i in test_list[ 0 :: 3 ]]
print ("The list after editing every kth element : "
+ str (test_list))
|
Output:
The original list is : [1, 4, 5, 6, 7, 8, 9, 12]
The list after editing every kth element : [3, 4, 5, 8, 7, 8, 11, 12]
Time Complexity: O(n), where n is the length of test_list
Auxiliary Space: O(1)
Method #3:Using range function
Python3
test_list = [ 1 , 4 , 5 , 6 , 7 , 8 , 9 , 12 ]
print ( "The original list is :" + str (test_list))
for i in range ( 0 , len (test_list), 3 ):
test_list[i] = test_list[i] + 2
print ( "The list after editing every kth element:" + str (test_list))
|
Output
The original list is :[1, 4, 5, 6, 7, 8, 9, 12]
The list after editing every kth element:[3, 4, 5, 8, 7, 8, 11, 12]
Time Complexity: O(n), where n is the length of the list, since we are iterating over each element of the list once.
Auxiliary Space: O(1), since we are not creating any new data structures that scale with the size of the input. We are simply modifying the existing list in place.
Method #4:Using itertools module and the islice function
To operate on every Kth element of a list , you can use the itertools module and the islice function. Here is an example of how to add 2 to every 3rd element of a list using islice:
Python3
from itertools import islice
test_list = [ 1 , 4 , 5 , 6 , 7 , 8 , 9 , 12 ]
print ( "The original list is:" , test_list)
test_list[:: 3 ] = [x + 2 for x in islice(test_list, 0 , None , 3 )]
print ( "The modified list is:" , test_list)
|
Output
The original list is: [1, 4, 5, 6, 7, 8, 9, 12]
The modified list is: [3, 4, 5, 8, 7, 8, 11, 12]
Time complexity: O(n), where n is the length of the list.
Auxiliary space: O(n), where n is the length of the list.
Method #5: Using a for loop
Here is the step-by-step algorithm for implementing the approach in the code:
- Initialize a list test_list with some integer values.
- Print the original list test_list.
- Use a for loop to iterate through the indices of test_list in steps of 3.
- Within the loop, add 2 to the value at the current index.
- Print the modified list test_list.
Python3
test_list = [ 1 , 4 , 5 , 6 , 7 , 8 , 9 , 12 ]
print ( "The original list is:" , test_list)
for i in range ( 0 , len (test_list), 3 ):
test_list[i] + = 2
print ( "The modified list is:" , test_list)
|
Output
The original list is: [1, 4, 5, 6, 7, 8, 9, 12]
The modified list is: [3, 4, 5, 8, 7, 8, 11, 12]
Time complexity: O(n), where n is the length of the input list. This is because the for loop iterates over each index in the list once, and the addition operation takes constant time. Therefore, the time taken by the algorithm grows linearly with the size of the input.
Auxiliary space: O(1), which is constant space complexity. This is because the algorithm only modifies the original input list and does not create any additional data structures. Therefore, the space taken by the algorithm remains constant regardless of the size of the input.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...