Given a List, find maximum of next K elements from each index.
Input : test_list = [4, 3, 9, 2, 6, 12, 4, 3, 2, 4, 5], K = 4
Output : [9, 9, 9, 12, 12, 12, 4, 5]
Explanation : Max of next 4 elements, (max(4, 3, 9, 2) = 9)
Input : test_list = [4, 3, 9, 2, 6], K = 4
Output : [9, 9]
Explanation : Max of next 4 elements, (max(4, 3, 9, 2) = 9)
In this, we iterate for elements in loop, slicing till next K, and use max() to get maximum of them for current index.
OutputThe original list is : [4, 3, 8, 2, 6, 7, 4, 3, 2, 4, 5]
Next K Maximum List : [8, 8, 8, 7, 7, 7, 4, 5]
This is yet another way to solve this, one-liner alternative to above method using list comprehension.
OutputThe original list is : [4, 3, 8, 2, 6, 7, 4, 3, 2, 4, 5]
Next K Maximum List : [8, 8, 8, 7, 7, 7, 4, 5]
OutputThe original list is : [4, 3, 8, 2, 6, 7, 4, 3, 2, 4, 5]
Next K Maximum List : [8, 8, 8, 7, 7, 7, 4, 5]