Python | Consecutive Subsets Minimum
Last Updated :
12 Jul, 2025
Some of the classical problems in programming domain comes from different categories and one among them is finding the minimum of subsets. This particular problem is also common when we need to accumulate the minimum and store consecutive group minimum. Let’s try different approaches to this problem in python language.
Method #1 : Using list comprehension + min() The list comprehension can be used to perform the this particular task to filter out successive groups and min function can be used to get the minimum of the filtered solution.
Python3
# Python3 code to demonstrate
# Consecutive Subsets Minimum
# using list comprehension + min()
# initializing list
test_list = [4, 7, 8, 10, 12, 15, 13, 17, 14]
# printing original list
print("The original list : " + str(test_list))
# using list comprehension + min()
# Consecutive Subsets Minimum
res = [ min(test_list[x : x + 3]) for x in range(0, len(test_list), 3)]
# printing result
print("The grouped minimum list is : " + str(res))
Output : The original list : [4, 7, 8, 10, 12, 15, 13, 17, 14]
The grouped minimum list is : [4, 10, 13]
Time Complexity: O(n) where n is the number of elements in the in the list “test_list”. The list comprehension + min() is used to perform the task and it takes O(n) time.
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the in the list “test_list”.
Method #2 : Using min() + itertools.islice() The task of slicing the list into chunks is done by islice method here and the conventional task of getting the minimum is done by the min function as the above method.
Python3
# Python3 code to demonstrate
# Consecutive Subsets Minimum
# using itertools.islice() + min()
import itertools
# initializing list
test_list = [4, 7, 8, 10, 12, 15, 13, 17, 14]
# printing original list
print("The original list : " + str(test_list))
# using itertools.islice() + min()
# Consecutive Subsets Minimum
res = [min(list(itertools.islice(test_list, i, i + 3))) for i in range(0, len(test_list), 3)]
# printing result
print("The grouped minimum list is : " + str(res))
Output : The original list : [4, 7, 8, 10, 12, 15, 13, 17, 14]
The grouped minimum list is : [4, 10, 13]
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 list “test_list”.
Method #3 : Using numpy.array_split() + min()
Note: Install numpy module using command "pip install numpy"
In this approach, we can use the numpy library's array_split() function to split the original list into consecutive chunks of a specified size and then use the min() function to find the minimum value in each chunk.
Python3
import numpy as np
# initializing list
test_list = [4, 7, 8, 10, 12, 15, 13, 17, 14]
# printing original list
print("The original list : " + str(test_list))
# Using numpy.array_split() + min()
# Consecutive Subsets Minimum
res = [min(chunk) for chunk in np.array_split(test_list, len(test_list)//3)]
# printing result
print("The grouped minimum list is : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy
Output:
The original list : [4, 7, 8, 10, 12, 15, 13, 17, 14]
The grouped minimum list is : [4, 10, 13]
Time Complexity: O(n) where n is the number of elements in the string list. The numpy is used to perform the task and it takes O(n) time.
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the test list.
Method #4 :Using heapq:
Algorithm:
- Initialize an empty list res.
- Loop through the original list in increments of 3.
- For each 3 consecutive elements, use the nsmallest function from heapq to get the smallest element and append it to res.
- Print the res list.
Python3
# Python3 code to demonstrate
# Consecutive Subsets Minimum
# using heapq
import heapq
# initializing list
test_list = [4, 7, 8, 10, 12, 15, 13, 17, 14]
# printing original list
print("The original list : " + str(test_list))
# using heapq
# Consecutive Subsets Minimum
res = []
for i in range(0, len(test_list), 3):
res.append(heapq.nsmallest(1, test_list[i:i+3])[0])
# printing result
print("The grouped minimum list is : " + str(res))
OutputThe original list : [4, 7, 8, 10, 12, 15, 13, 17, 14]
The grouped minimum list is : [4, 10, 13]
Time complexity: O(nlogk), where n is the length of the input list and k is the size of the subset. Here k is 3, so the time complexity is O(nlog3), which can be simplified to O(n).
Space complexity: O(k), where k is the size of the subset. Here k is 3, so the space complexity is O(3), which can be simplified to O(1).
Similar Reads
Python - Maximum element in consecutive subsets We are given a list of numbers and a fixed integer k. The task is to determine the maximum number in every consecutive block of k elements. For instance, consider the list [1,3,2,5,4,6,8,7] with k=3. This list can be divided into consecutive subsets: the first subset is [1,3,2], the next is [3,2,5]
3 min read
Python - Maximum element in consecutive subsets We are given a list of numbers and a fixed integer k. The task is to determine the maximum number in every consecutive block of k elements. For instance, consider the list [1,3,2,5,4,6,8,7] with k=3. This list can be divided into consecutive subsets: the first subset is [1,3,2], the next is [3,2,5]
3 min read
Python | Consecutive Pair Minimums Sometimes, while working with Python list, one can have a problem in which one needs to find perform the minimum 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 solved.
4 min read
Python - K consecutive Maximum 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 o
4 min read
Python - Minimum identical consecutive Subarray Sometimes, while working with Python lists or in competitive programming setup, we can come across a subproblem in which we need to get an element that has the minimum consecutive occurrence. The knowledge of solution to it can be of great help and can be employed whenever required. Letâs discuss a
3 min read
Python | Minimum Sum of Consecutive Characters Sometimes, we might have a problem in which we require to get the minimum sum of 2 numbers from list but with the constraint of having the numbers in successions. This type of problem can occur while competitive programming. Letâs discuss certain ways in which this problem can be solved. Method #1:
5 min read