Lists are very commonly used to store sequences of values (for example, numbers) in Python. When working with lists, it can be handy to know how to quickly get the sum of values in a list. For example, you have a list of your recorded footsteps in the last seven days and you want to know the total sum. In this tutorial, we will look at how to get the sum of the elements in a list in Python with the help of some examples.
How to get the sum of a list of numbers in Python?
You can use the python built-in sum()
function to get the sum of list elements. Alternatively, you can use a loop to iterate through the list items and use a variable to keep track of the sum.
Let’s look at the above-mentioned methods with the help of some examples.
Using sum()
to get the total in a list
The built-in sum()
function in Python is used to return the sum of an iterable. To get the sum total of a list of numbers, you can pass the list as an argument to the sum()
function.
# create a list ls = [10, 15, 20, 25] # sum of list elements sum(ls)
Output:
70
We get the sum of the values in the list as a scaler value.
Note that the sum()
function may result in loss of precision with extended sums of floating-point numbers. For example –
# create a list ls = [0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1] # sum of list elements sum(ls)
Output:
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
0.8999999999999999
As an alternative, you can use the math
standard library’s fsum()
function to get an accurate sum of floating-point numbers and prevent loss of precision.
import math # create a list ls = [0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1] # sum of list elements math.fsum(ls)
Output:
0.9
We get the accurate result this time. For more on the fsum()
function, refer to its documentation.
Using loop to get the sum
Alternatively, you can use the straightforward method of iterating through the list elements and keeping track of the sum.
# create a list ls = [10, 15, 20, 25] # use a loop to get the sum total = 0 for item in ls: total += item print(total)
Output:
70
We get the sum of the values in the list.
You might also be interested in –
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.