Signup/Sign In

4 Ways to Find average of a list in python

Posted in Programming   LAST UPDATED: NOVEMBER 10, 2022

    4 ways to find the average of a list using Python

    You can find an average of a list with the help of Python. Average is the sum of all the elements divided by the number of elements.

    In order to find the average for a given list, you will need to use some Python functions like-

    • sum() function
    • len() function
    • round() function
    • reduce(), lambda
    • mean().
    Input : [1,2,4,5,6,7,8]
    Output : Average of the list = 4.71
    
    Explanation:
    Sum of the elements is 1+2+4+5+6+7+8 = 33
    and total number of elements is 7.
    So average is 33 / 7 = 4.71
    

    1). How to Find Average of a List in Python using the sum() function

    This simple method uses the sum() and len() functions to calculate the average.

    • Sum() : Sum function returns the addition of the list
    • Len() : len() function returns the length of the list(number of elements the list has).
    # Python program to get average of a list
    def Average(lst):
    	return sum(lst) / len(lst)
    
    # Driver Code
    lst = [1,2,4,5,6,7,8]
    average = Average(lst)
    
    # Printing average of the list
    print("Average of the list =", round(average, 2))
    

    Try this Code Yourself

    If you don't know the sum() function, you can learn it with our free Interactive course.

    2). How to Find the Average of a List in Python using the reduce() function and lambda.

    We can calculate the average of a list using reduce() and Lambda. Reduce function reduces the loop and lambda is used to calculate the sum of the list.

    # Python program to get average of a list
    # Using reduce() and lambda
    
    # importing reduce()
    from functools import reduce
    
    def Average(lst):
    	return reduce(lambda a, b: a + b, lst) / len(lst)
    
    # Driver Code
    lst = [1,2,4,5,6,7,8]
    average = Average(lst)
    
    # Printing average of the list
    print("Average of the list =", round(average, 2))
    

    Try this Code Yourself

    3). How to Find Average of a List in Python using the mean() function

    Python has a mean() function that directly calculates the average of the list. You can use it to find the average of a list.

    # Python program to get average of a list
    # Using mean()
    
    # importing mean()
    from statistics import mean
    
    def Average(lst):
    	return mean(lst)
    
    # Driver Code
    lst = [1,2,4,5,6,7,8]
    average = Average(lst)
    
    # Printing average of the list
    print("Average of the list =", round(average, 2))
    

    Try this Code Yourself

    4). How to Find Average of a List in Python by iterating over the list

    You can iterate over the list using for loop and by doing some operations on each of the elements, you can get the average of the list.

    # Python code to get average of list
    def Average(lst):
    	sum_of_list = 0
    	for i in range(len(lst)):
    		sum_of_list += lst[i]
    	average = sum_of_list/len(lst)
    	return average
    
    
    # Driver Code
    lst = [1,2,4,5,6,7,8]
    average = Average(lst)
    print("Average of the list =", round(average, 2))
    

    Try this Code Yourself

    Related Questions

    1). How do you average a list in a for loop python?

    num_list = [1,2,3,4,5]
    r = 0
    for num in num_list:
        res1 += num
    avg1 = r / len(num_list)
    print("sum is: ", r, "Average is: ", avg1)
    

    2). Is there an average function in Python?

    There's not an average function in Python but there is a mean() function to calculate mean/average of input values

    About the author:
    Proficient in Java, Python, and web development; has a knack for writing clearly about complex topics. Dedicated to giving readers the tools they need to learn more about computer science and technology.
    Tags:list-operationpython-programpython
    IF YOU LIKE IT, THEN SHARE IT
     

    RELATED POSTS