• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Using Python to Calculate Average of List of Numbers

Using Python to Calculate Average of List of Numbers

August 19, 2022 Leave a Comment

To calculate the average of a list of numbers in Python, the easiest way is to divide the sum of the list by the length of the list with the sum() and len() functions.

l = [1,2,3,4,5,6,7]

print(sum(l)/len(l))

#Output:
4

You can also use the mean() function from the statistics module to get the average of a list.

from statistics import mean

l = [1,2,3,4,5,6,7]

print(mean(l))

#Output:
4

You can also use a for loop to sum the numbers of a list and then divide by the length of the list to get the mean of a list in Python.

l = [1,2,3,4,5,6,7]

def mean(lst):
    s = 0
    for num in lst:
        s = s + num
    return s / len(lst)

print(mean(l))

#Output:
4

When working with collections of data in Python, the ability to summarize the data easily is valuable.

One such case is if you want to get the average of a list of numbers.

To get the average of a list, you get the sum of the list and divide by the number of items in the list.

To calculate the average of a list of numbers in Python, the easiest way is with the sum() and len() functions.

sum() returns the sum of a list of numbers, and len() returns the length of the list.

Below shows you how to get the average of a list of numbers in Python with sum() and len().

l = [1,2,3,4,5,6,7]

print(sum(l)/len(l))

#Output:
4

Using statistics Module mean Function to Get Average of List in Python

A useful module in Python is the statistics module. The statistics module has many great functions for performing different calculations.

One such calculation is finding the mean of a collection of data.

You can use the statistics module mean() function to get the average of a list in Python.

Below shows you how to use mean() to get the average of a list in Python.

from statistics import mean

l = [1,2,3,4,5,6,7]

print(mean(l))

#Output:
4

Using For Loop to Get Average of List in Python

Another way you can get the mean of numbers of a list is with a for loop to get the sum and then use len() to get the length of the list.

To add the numbers of a list up, initialize a variable which will keep the running sum and then add each element to the running sum.

Then divide by the length of the list.

Below shows you how to get the average of a list of numbers in Python with a for loop.

l = [1,2,3,4,5,6,7]

def mean(lst):
    s = 0
    for num in lst:
        s = s + num
    return s / len(lst)

print(mean(l))

#Output:
4

Hopefully this article has been useful for you to learn how to get the average of a list in Python.

Other Articles You'll Also Like:

  • 1.  Get Last N Elements of List in Python
  • 2.  Write Variable to File Using Python
  • 3.  Writing a Table Faster to Word Document Using python-docx
  • 4.  Find Index of Minimum in List Using Python
  • 5.  Write Float to File Using Python
  • 6.  Get Random Value from Dictionary in Python
  • 7.  Using Python to Find Maximum Value in List
  • 8.  Get First Key and Value in Dictionary with Python
  • 9.  Transpose DataFrame pandas – Using the pandas transpose Function
  • 10.  Python math.factorial() function – Calculate Factorial of Number

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy