Python | Consecutive Subsets Minimum

• Last Updated : 19 Feb, 2020

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 code to demonstrate# Consecutive Subsets Minimum# using list comprehension + min()  # initializing listtest_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 Minimumres = [ min(test_list[x : x + 3]) for x in range(0, len(test_list), 3)]  # printing resultprint("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]

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 code to demonstrate# Consecutive Subsets Minimum# using itertools.islice() + min()import itertools  # initializing listtest_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 Minimumres = [min(list(itertools.islice(test_list, i, i + 3))) for i in range(0, len(test_list), 3)]  # printing resultprint("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]

My Personal Notes arrow_drop_up