Open In App

Python | Operate on every Kth element of list

Last Updated : 25 Apr, 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 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




# Python3 code to demonstrate
# Edit every Kth element 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))
 
# using list comprehension + enumerate()
# Edit every Kth element in list
# add 2 to every 3rd element
res = [i + 2 if j % 3 == 0 else i
    for j, i in enumerate(test_list)]
 
# printing result
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




# Python3 code to demonstrate
# Edit every Kth element 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))
 
# using list comprehension + list slicing
# Edit every Kth element in list
# add 2 to every 3rd element
test_list[0::3] = [i + 2 for i in test_list[0 :: 3]]
 
# printing result
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




# Python3 code to demonstrate
# Edit every Kth element in list
 
# initializing list
test_list = [1, 4, 5, 6, 7, 8, 9, 12]
 
# printing the original list
print("The original list is :" + str(test_list))
 
for i in range(0, len(test_list), 3):
    # Edit every Kth element in list
    # add 2 to every 3rd element
    test_list[i] = test_list[i]+2
 
 
# printing result
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
 
# Initialize the list
test_list = [1, 4, 5, 6, 7, 8, 9, 12]
 
# Print the original list
print("The original list is:", test_list)
 
# Use islice and itertools to add 2 to every 3rd element
test_list[::3] = [x+2 for x in islice(test_list, 0, None, 3)]
 
# Print the modified list
print("The modified list is:", test_list)
#This code is contributed by Edula Vinay Kumar Reddy


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:

  1. Initialize a list test_list with some integer values.
  2. Print the original list test_list.
  3. Use a for loop to iterate through the indices of test_list in steps of 3.
  4. Within the loop, add 2 to the value at the current index.
  5. Print the modified list test_list.

Python3




# Initialize the list
test_list = [1, 4, 5, 6, 7, 8, 9, 12]
 
# Print the original list
print("The original list is:", test_list)
 
# Add 2 to every 3rd element using a for loop
for i in range(0,len(test_list), 3):
    test_list[i] += 2
 
# Print the modified list
print("The modified list is:", test_list)
#This code is contributed Vinay Pinjala.


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
Previous
Next
Share your thoughts in the comments

Similar Reads