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 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 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)) |
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]
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 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)) |
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]
Recommended Posts:
- Python | Replace elements in second list with index of same element in first list
- Python | Indices list of matching element from other list
- Python | Slicing list from Kth element to last element
- Python | Move one list element to another list
- Python | How to get the last element of list
- Python | Every Kth element in list
- Python | Three element sum in list
- Python | Remove given element from the list
- Python | Last occurrence of some element in a list
- Python | Insert after every Nth element in a list
- Python | Equidistant element list
- Python | Move element to end of the list
- Python | Element repetition in list
- Python | Add the element in the list with help of indexing
- Python | Removing first element of list
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.



