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 - Extend consecutive tuples
Next article icon

Python | Mean of consecutive Sublist

Last Updated : 24 Mar, 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 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 python language. 

Method #1 : Using list comprehension + sum() The list comprehension can be used to perform the this particular task to filter out successive groups and sum function can be used to get the summation of the filtered solution. We divide the sum by sublist size for average. 

Python3




# Python3 code to demonstrate
# Mean of consecutive Sublist
# using list comprehension + sum()
 
# 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 + sum()
# Mean of consecutive Sublist
res = [ sum(test_list[x : x + 3]) / 3 for x in range(0, len(test_list), 3)]
 
# printing result
print("The grouped average list is : " + str(res))
 
 
Output : 
The original list : [4, 7, 8, 10, 12, 15, 13, 17, 14] The grouped average list is : [6.333333333333333, 12.333333333333334, 14.666666666666666]

Time Complexity: O(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 #2 : Using sum() + itertools.islice() The task of slicing the list into chunks is done by islice method here and the conventional task of getting the summation is done by the sum function as the above method. We divide the sum by sublist size for average. 

Python3




# Python3 code to demonstrate
# Mean of consecutive Sublist
# using itertools.islice() + sum()
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() + sum()
# Mean of consecutive Sublist
res = [sum(list(itertools.islice(test_list, i, i + 3))) / 3 for i in range(0, len(test_list), 3)]
 
# printing result
print("The grouped average list is : " + str(res))
 
 
Output : 
The original list : [4, 7, 8, 10, 12, 15, 13, 17, 14] The grouped average list is : [6.333333333333333, 12.333333333333334, 14.666666666666666]

Time complexity: O(n), where n is the length of the input list ‘test_list’

Space complexity: O(n), where n is the length of the input list ‘test_list’

Method #3 : Using numpy.mean()

We can use the mean function from numpy library which will help us to get the mean of elements. We pass the argument list converted to numpy array and chunksize as argument to mean function.

Python3




# Python3 code to demonstrate
# Mean of consecutive Sublist
# using numpy.mean()
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.mean()
# Mean of consecutive Sublist
res = [np.mean(np.array(test_list[i:i+3])) for i in range(0, len(test_list), 3)]
   
# printing result
print("The grouped average list is : " + str(res))
 
 

Output:

The original list : [4, 7, 8, 10, 12, 15, 13, 17, 14]
The grouped average list is : [6.333333333333333, 12.333333333333334, 14.666666666666666]
 

Time complexity: O(n)

Space complexity: O(n)



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

Similar Reads

  • Python | Consecutive Subsets Minimum
    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
    4 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 - 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
  • Python - Test Consecutive Element Matrix
    Given a Matrix, test if it is made of consecutive elements. Input : test_list = [[4, 5, 6], [7], [8, 9, 10], [11, 12]] Output : True Explanation : Elements in Matrix range from 4 to 12. Input : test_list = [[4, 5, 6], [7], [8, 18, 10], [11, 12]] Output : False Explanation : Elements not consecutive
    6 min read
  • Python - Summation of consecutive elements power
    Given a List, the task is to write a Python program to compute summation of power of consecutive elements occurrences frequency. Examples: Input : test_list = [2, 2, 2, 3, 3, 3, 3, 4, 4, 5] Output : 110 Explanation : 2^3 + 3^4 + 4^2 + 5 = 110 Input : test_list = [2, 2, 2, 3, 3, 3, 3, 4, 4] Output :
    3 min read
  • Python - Consecutive Missing elements Sum
    Sometimes, we can get elements in range as input but some values are missing in otherwise consecutive range. We might have a use case in which we need to get a summation of all the missing elements. Let’s discuss certain ways in which this can be done. Method #1 : Using list comprehension + sum() We
    5 min read
  • Python | Retain K consecutive elements
    Sometimes while working with data, we can have a problem in which we need to select some of the elements that occur K times consecutively. This problem can occur in many domains. Let's discuss certain ways in which this problem can be solved. Method #1 : Using groupby() + list comprehension This tas
    8 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 - Count frequency of Sublist in given list
    Given a List and a sublist, count occurrence of sublist in list. Input : test_list = [4, 5, 3, 5, 7, 8, 3, 5, 7, 2, 3, 5, 7], sublist = [3, 5, 7] Output : 3 Explanation : 3, 5, 7 occurs 3 times. Input : test_list = [4, 5, 3, 5, 8, 8, 3, 2, 7, 2, 3, 6, 7], sublist = [3, 5, 7] Output : 0 Explanation :
    3 min read
  • Python - Merge elements of sublists
    In Python, we often need to combine multiple sublists into a single list. This can be useful when dealing with data that is organized in smaller chunks, like rows in a table or pages in a document. Using itertools.chain()itertools.chain() function is one of the most efficient ways to merge sublists.
    4 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