How to Sum a List of Numbers in Python

The best way to sum a list of numbers in Python is to use the Python built-in sum() function. You can pass list to the sum() function is a parameter and it will return the sum of a number in a list as an integer.

Besides the sum function, there are also other methods which we will discuss in this article: so let’s get start:

Using sum() function

Python provides a built-in function called sum() that can be used to sum a list of numbers.

numbers = [1, 2, 3, 4, 5]
sum = sum(numbers)
print("The sum is:", sum)
# The sum is: 15

Using a for loop

The most straightforward way to sum a list of numbers in Python is by using a for loop.

numbers = [1, 2, 3, 4, 5]
sum = 0
for i in numbers:
    sum += i
print("The sum is:", sum)
# The sum is: 15

Using the reduce() function

The reduce() function from the functools the module can also be used to sum up a list of numbers.

from functools import reduce
numbers = [1, 2, 3, 4, 5]
sum = reduce((lambda x, y: x + y), numbers)
print("The sum is:", sum)
# The sum is: 15

Summary and Conclusion

In this article, we have learned how to find the sum of numbers in a list. I hope this article was helpful. if you have any questions please leave them in the comment section.

Leave a Comment

Scroll to Top