This pithy, example-based article will walk you through a couple of different ways to calculate the sum of a numeric list (a list that contains only numbers) in Python. We’ll also discuss the performance of each approach so you can know how fast and efficient it is.
Using the sum() function
I think this is the go-to approach for most cases. It is concise, readable, and efficient. What you need to do is just call the built-in
sum() function on your list and assign the result to a variable. Note that if your list is empty or contains non-numeric elements, a
TypeError will be raised.
numbers = [1, 1.5, 2, 2.4, 3, 3.5, 4, 4.5, 5, 5.5] result = sum(numbers) print(result)
More about performance: The
sum() function has a time complexity of
n is the length of the list. It has a space complexity of
O(1), as it does not create any new data structures.
Using a for loop
This is a more verbose, manual, and iterative way to find the sum of a numeric list in Python. It is less readable and Pythonic than the preceding one. However, it gives more control over the logic and error handling. It can be customized to handle different types of elements or conditions in the list.
numbers = [1, 1.5, 2, 2.4, 3, 3.5, 4, 4.5, 5, 5.5] result = 0 for number in numbers: if(type(number) == int or type(number) == float): result += number print(result)
The performance of this method is similar to the built-in
sum() function. The
for loop has a time complexity of
O(n), where n is the length of the list. It has a space complexity of
O(1), as it only uses one extra variable to store the sum.
Using the reduce() function
You can use the
reduce() function to apply a function to each pair of elements in the list and reduce them to a single value. In the context of this article, that single value is the sum we want to find. Note that you need to import the
functools module before calling the
# Import the functools module import functools # Define a custom function that takes two arguments and returns their sum def add(x, y): return x + y numbers = [1, 1.5, 2, 2.4, 3, 3.5, 4, 4.5, 5, 5.5] total = functools.reduce(add, numbers) # Print the sum print(total)
Regarding performance, the
reduce() function has a time complexity of
O(n) and its space complexity is
O(1). Therefore, it’s fast and efficient, almost like the two earlier solutions.
There’re still more ways to achieve the goal. However, they are not very different from the ones I showed you above. hey may have slightly different syntax or performance, but the logic is nearly identical.
For example, you can use a
while loop instead of a
for loop like this:
numbers = [1, 1.5, 2, 2.4, 3, 3.5, 4, 4.5, 5, 5.5] # Initialize an index variable to zero i = 0 # get the length of the list count = len(numbers) # Initialize a variable to hold the total total = 0 while i < count: total += numbers[i] i += 1 print(total)
We’ve walked through several ways to compute the total of all elements in a numeric list in Python. In general, the first approach is the best choice in terms of both code readability and performance. The others might be used less often, but they are worth knowing. This tutorial ends here. Happy coding & enjoy your day!