Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Python - K consecutive Maximum
Next article icon

Python | Consecutive Subsets Minimum

Last Updated : 04 May, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

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:

  1. Initialize an empty list res.
  2. Loop through the original list in increments of 3.
  3. For each 3 consecutive elements, use the nsmallest function from heapq to get the smallest element and append it to res.
  4. 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))
 
 
Output
The 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).



Next Article
Python - K consecutive Maximum
author
manjeet_04
Improve
Article Tags :
  • Python
  • Python Programs
  • Python list-programs
Practice Tags :
  • python

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 | 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.
    5 min read
  • Python | Mean of consecutive Sublist
    Some of the classical problems in programming domain comes from different categories and one among them is finding the mean of subsets. This particular problem is also common when we need to compute the average and store consecutive group mean. Let’s try different approaches to this problem in pytho
    3 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
  • Python - Extend consecutive tuples
    Given list of tuples, join consecutive tuples. Input : test_list = [(3, 5, 6, 7), (3, 2, 4, 3), (9, 4), (2, 3, 2)] Output : [(3, 5, 6, 7, 3, 2, 4, 3), (3, 2, 4, 3, 9, 4), (9, 4, 2, 3, 2)] Explanation : Elements joined with their consecutive tuples. Input : test_list = [(3, 5, 6, 7), (3, 2, 4, 3)] Ou
    3 min read
  • Python - Squash consecutive values of K
    Sometimes, while working with Python lists, we can have a problem in which we need to perform the removal of duplicate consecutive values of a particular element. This kind of problem can have applications in many domains such as competitive programming and web development. Let's discuss certain way
    3 min read
  • Python - Consecutive Tuple difference
    Given List of tuples, find index-wise absolute difference of consecutive tuples. Input : test_list = [(5, 3), (1, 4), (9, 5), (3, 5)] Output : [(4, 1), (7, 1), (6, 0)] Explanation : 5 - 1 = 4, 4 - 3 = 1. hence (4, 1) and so on. Input : test_list = [(9, 5), (3, 5)] Output : [(6, 0)] Explanation : 9 -
    4 min read
  • Python - Filter consecutive elements Tuples
    Given a Tuple list, filter tuples that are made from consecutive elements, i.e diff is 1. Input : test_list = [(3, 4, 5, 6), (5, 6, 7, 2), (1, 2, 4), (6, 4, 6, 3)] Output : [(3, 4, 5, 6)] Explanation : Only 1 tuple adheres to condition. Input : test_list = [(3, 4, 5, 6), (5, 6, 7, 2), (1, 2, 3), (6,
    5 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences