Python - Every Kth index Maximum in List
Last Updated :
18 Apr, 2023
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 Kth element in list. Let’s discuss certain ways in which maximum of these elements can be performed.
Method #1 : Using list comprehension + enumerate() + max() 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. The max() helps to find max.
Python3
# Python3 code to demonstrate
# Every Kth index Maximum in List
# using list comprehension + enumerate() + max()
# initializing list
test_list = [1, 4, 5, 6, 7, 8, 9, 12]
# printing the original list
print ("The original list is : " + str(test_list))
# initializing K
K = 3
# using list comprehension + enumerate() + max()
# Every Kth index Maximum in List
# max of every 3rd element
res = max([i for j, i in enumerate(test_list) if j % K == 0 ])
# printing result
print ("The max of every kth element : " + str(res))
Output : The original list is : [1, 4, 5, 6, 7, 8, 9, 12]
The max of every kth element : 9
Time Complexity: O(n*n) where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n), where n is the number of elements in the new 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
# The max() helps to find max.
# using list comprehension + list slicing + max()
# 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 + max()
# Edit every Kth element in list
# max of every 3rd element
res = max(test_list[0::3])
# printing result
print ("The max of every kth element : " + str(res))
Output : The original list is : [1, 4, 5, 6, 7, 8, 9, 12]
The max of every kth element : 9
Time Complexity : O(n/3), where n is the length of test_list. As list slicing iterates over every 3rd element in the list.
Auxiliary Space : O(1), as only additional single space is used for res variable.
Method #3:Using for loop
This code finds the maximum value among every 3rd element in the given list test_list.
Here is the step-by-step algorithm:
- Initialize the list test_list.
- Initialize a variable max_elem to negative infinity.
- Loop through every 3rd element in the list using a for loop and range() function.
- For each element, check if it is greater than the current max_elem. If it is, update max_elem.
- After the loop, max_elem will contain the maximum value among every 3rd element in the list.
- Print the result.
Python3
# Python3 code to demonstrate
# The max() helps to find max.
# using for loop
# 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 for loop to find max of every 3rd element
max_elem = float('-inf') # initialize max_elem to negative infinity
for i in range(0,len(test_list), 3):
if test_list[i] > max_elem:
max_elem = test_list[i]
# printing result
print("The max of every kth element : " + str(max_elem))
#This code contributed by vinay pinjala.
OutputThe original list is : [1, 4, 5, 6, 7, 8, 9, 12]
The max of every kth element : 9
The time complexity of this code is O(N/3), where N is the length of test_list. This is because the for loop iterates only over every 3rd element in the list.
The auxiliary space of the code is O(1), because the only additional space used is for the max_elem variable, which is a single integer.
Similar Reads
Python - K Maximum elements with Index in List GIven a List, extract K Maximum elements with their indices. Input : test_list = [5, 3, 1, 4, 7, 8, 2], K = 2 Output : [(4, 7), (5, 8)] Explanation : 8 is maximum on index 5, 7 on 4th. Input : test_list = [5, 3, 1, 4, 7, 10, 2], K = 1 Output : [(5, 10)] Explanation : 10 is maximum on index 5. Method
4 min read
Python - Elements Maximum till current index in List Given list with elements, extract element if it's the maximum element till current index. Input : test_list = [4, 6, 7, 8] Output : [4, 6, 7, 8] Explanation : All elements are maximum till their index. Input : test_list = [6, 7, 3, 6, 8, 7] Output : [7, 8] Explanation : 7 and 8 are maximum till thei
7 min read
Python | Maximum element in tuple list Sometimes, while working with data in form of records, we can have a problem in which we need to find the maximum element of all the records received. This is a very common application that can occur in Data Science domain. Letâs discuss certain ways in which this task can be performed. Method #1: U
6 min read
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 sli
3 min read
Python - Maximum element in Cropped List Sometimes, while working with Python, we can have a problem in which we need to get maximum of list. But sometimes, we need to get this for between custom indices. This can be need of any domain be it normal programming or web development. Let's discuss certain ways in which this task can be perform
4 min read
Python - Maximum of Consecutive Pair in integer list Sometimes, while working with Python list, one can have a problem in which one needs to find perform the maximization of list in pair form. This is useful as a subproblem solution of bigger problem in web development and day-day programming. Letâs discuss certain ways in which this problem can be so
5 min read