Skip to content
Fix Code Error

Finding the average of a list

March 13, 2021 by Code Error
Posted By: Anonymous

I have to find the average of a list in Python. This is my code so far

l = [15, 18, 2, 36, 12, 78, 5, 6, 9]
print reduce(lambda x, y: x + y, l)

I’ve got it so it adds together the values in the list, but I don’t know how to make it divide into them?

Solution

On Python 3.4+ you can use statistics.mean()

l = [15, 18, 2, 36, 12, 78, 5, 6, 9]

import statistics
statistics.mean(l)  # 20.11111111111111

On older versions of Python you can do

sum(l) / len(l)

On Python 2 you need to convert len to a float to get float division

sum(l) / float(len(l))

There is no need to use reduce. It is much slower and was removed in Python 3.

Answered By: Anonymous

Related Articles

  • How do I include certain conditions in SQL Count
  • problem with client server unix domain stream…
  • Rails wrong number of arguments error when…
  • Python is not calling fucntions properly
  • get an unknown amount of numbers from the user and…
  • 'block in draw' rails 6 routes
  • Fastest way to iterate over all the chars in a String
  • data.table vs dplyr: can one do something well the…
  • How to compute precision, recall, accuracy and…
  • When I'm testing a web app by JUnit and Mockito I…
  • Node.js Best Practice Exception Handling
  • Trouble with Next js + Express deployment using Zeit Now
  • Homebrew install specific version of formula?
  • How to sum each table of values in each index…
  • Apply multiple functions to multiple groupby columns
  • What makes the different performances between…
  • Array vs List in recursive F# function
  • Use awk to find average of a column
  • Adding up BigDecimals using Streams
  • Calculate the mean by group
  • Returning the product of a list
  • How do I merge two dictionaries in a single…
  • Finding the max sum of n consecutive numbers where…
  • Composition or Inheritance for classes with almost…
  • How do I break out of a loop in Scala?
  • Fix top buttons on scroll of list below
  • How can I manually compile a svelte component down…
  • Code to get sums made of a fibonacci number
  • Tailwind divide color only for divide-x
  • Good MapReduce examples
  • Creating an dynamic array, but getting segmentation…
  • There's an InputMismatchException, how do i fix it?
  • Recursive function prints garbage value while…
  • Smart way to truncate long strings
  • TypeError: ufunc 'add' did not contain a loop with…
  • How to query for Xml values and attributes from…
  • Function to calculate R2 (R-squared) in R
  • How do I install Java on Mac OSX allowing version switching?
  • How can I quantify difference between two images?
  • Most efficient way to generate Histograms of…
  • Jetpack Compose and Hilt Conflict
  • How to generate a random string of a fixed length in Go?
  • Find average value of variable in an interval in MATLAB
  • Union of multiple Database queries with same parameters
  • How can I print the report without the first in the list
  • Calculate emmeans using multiple values of the…
  • Kodein + Ktor = mutation attempt of frozen…
  • Expected expression before int
  • JUNIT @ParameterizedTest , Parameter resolution Exception
  • How to update Python?
  • Calculating the cumulative sum with a specific…
  • LINQ where clause with lambda expression having OR…
  • Calculate average in java
  • How to sum the values of a JavaScript object?
  • How to convert PHP Multidimensional Array to Json so…
  • How to enable named/bind/DNS full logging?
  • Polymer component iron-ajax make two requests
  • Importing a function from a class in another file?
  • When to use LinkedList over ArrayList in Java?
  • What's the difference between eval, exec, and compile?
  • python max function using 'key' and lambda expression
  • Not able to define Averageifs condition using…
  • How to sum multiple different objects in an array…
  • What's the function like sum() but for…
  • Passing string into a lambda function
  • How to declare an array inside MS SQL Server Stored…
  • how to print greatest sum each rows in python?
  • Vue CLI 3 / Webpack production build not working on…
  • average of 2 neighbouring elements in array python
  • Getting zeros for and or statement
  • How do I catch a numpy warning like it's an…
  • How to prevent click event for one component from another?
  • Issues with updating an inventory in Python Code
  • What is the difference between the following two…
  • Fast Python algorithm for finding the path that…
  • func init() vs func main() for initalizing global…
  • Why is the average of a million random numbers not…
  • How to return summed array from computed using…
  • Sorting a multidimensional array using merge sort?
  • How to parse JSON file with Spring
  • Joining and comparing values of one df with first…
  • Python Plotly Polar Chart Slice Alignment
  • Haskell: ternary tree average, with nested `where`
  • What does the star operator mean, in a function call?
  • What does "Fatal error: Unexpectedly found nil while…
  • Aurelia: Binding to computed values
  • Getting the closest string match
  • How do i calculate total without manually inputting it?
  • Python-coded neural network does not learn properly
  • How to find 2 parameters with gradient descent…
  • Identifying and solving…
  • Upper memory limit?
  • How to find the sum of an array of numbers
  • How do I obtain a Query Execution Plan in SQL Server?
  • Round number to nearest integer
  • How do I divide values across dataframes in pandas?
  • ClassNotFoundException thrown
  • How can make dynamic route to look like static route…
  • What is a lambda (function)?
  • Addition of two queries - SQL/HANA

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

Equivalent of shell ‘cd’ command to change the working directory?

Next Post:

Rename column SQL Server 2008

Leave a Reply Cancel reply

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

.net ajax android angular arrays aurelia backbone.js bash c++ css dataframe ember-data ember.js excel git html ios java javascript jquery json laravel linux list mysql next.js node.js pandas php polymer polymer-1.0 python python-3.x r reactjs regex sql sql-server string svelte typescript vue-component vue.js vuejs2 vuetify.js

  • you shouldn’t need to use z-index
  • No column in target database, but getting “The schema update is terminating because data loss might occur”
  • Angular – expected call-signature: ‘changePassword’ to have a typedeftslint(typedef)
  • trying to implement NativeAdFactory imports deprecated method by default in flutter java project
  • What should I use to get an attribute out of my foreign table in Laravel?
© 2022 Fix Code Error