Explain Codes LogoExplain Codes Logo

Finding the average of a list

python
performance
statistics
numpy
Anton ShumikhinbyAnton Shumikhin·Aug 11, 2024
TLDR

In Python, compute the average of a list using:

average = sum(numbers) / len(numbers)

Example:

numbers = [1, 2, 3, 4, 5] average = sum(numbers) / len(numbers) print(average) # Output: 3.0

Just remember: your list, numbers, must contain numerical values.

Boosting performance with fmean

Performance is key when dealing with large datasets. From Python 3.8 onwards, use statistics.fmean:

import statistics # As fast as a cheetah on a sugar rush! numbers = [1, 2, 3, 4, 5] average = statistics.fmean(numbers) print(average) # Output: 3.0

The fmean function minimizes error in floating-point arithmetic. Perfect for lists of floating-point numbers!

Keeping up with Python's evolution

Different Python versions offer varying methods for average calculations:

  • Python 3.4–3.7 favors statistics.mean—Call it a "crossfit workout" for numerical stability!

    import statistics # You're mean, I mean, mean numbers = [1, 2, 3, 4, 5] average = statistics.mean(numbers) print(average) # Output: 3
  • Python 2.x needs this to ensure floating-point division:

    numbers = [1, 2, 3, 4, 5] average = sum(numbers) / float(len(numbers)) print(average) # Does this Python make me look 2.0?

Remember, just like fashion, you've gotta keep up with Python's changes!

Making array calculations efficient with numpy

Working with large arrays? numpy.mean is your beast of burden:

import numpy as np # Now you're thinking with arrays numbers = np.array([1, 2, 3, 4, 5]) average = np.mean(numbers) print(average) # Output: 3.0

Remember, numpy's prowess lies in handling arrays. Bigger the better!

Precision in your pocket with the statistics module

The statistics module is a lifesaver when precise calculation is paramount:

import statistics # My prices are always mean... I mean average! prices = [19.99, 23.45, 35.89] average_price = statistics.mean(prices) print(average_price) # Always gets the mean right, even for cents!

Apart from decimal numbers, it's also great with monstrous datasets!

Handling miscreants: negative numbers and zero

In case of negative numbers or zeros, fear not! Our formula remains unfazed:

# Like water off a duck's back numbers = [5, -5, 0] average = sum(numbers) / len(numbers) print(average) # Output:0

Just remember, zeroes and negative numbers will bring your average down!

Dancing with data types

sum() gets along with integers and floats. Note that the output will be a float if there's a float in the list:

# Float like a butterfly, calculate like a bee numbers = [1.0, 2, 3] average = sum(numbers) / len(numbers) print(average) # Hey, where's the point in 2.0?

So, keep an eye on your data types!

Wrestling with nested lists

Nested list - Flatten it first!

import itertools # Flatten it like a pancake! numbers = [[1, 2], [3, 4]] flat_numbers = list(itertools.chain(*numbers)) average = sum(flat_numbers) / len(flat_numbers) print(average) # Output: 2.5

It ensures only numerical values enter the ring.