Open In App

Python – Every Kth index Maximum in List

Last Updated : 18 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 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:

  1. Initialize the list test_list.
  2. Initialize a variable max_elem to negative infinity.
  3. Loop through every 3rd element in the list using a for loop and range() function.
  4. For each element, check if it is greater than the current max_elem. If it is, update max_elem.
  5. After the loop, max_elem will contain the maximum value among every 3rd element in the list.
  6. 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.


Output

The 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.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads