Sum Elements
Using for loops to sum elements in Python.
2 min read |358 words
March 18, 2026Summing Elements in Python
In Python, you can sum elements in a list using various methods. Here are some common approaches:
Using a For Loop
numbers = [1, 2, 3, 4, 5]
total = 0
for number in numbers:
total += number
print(total) # Output: 15
Using the Built-in sum() Function
numbers = [1, 2, 3, 4, 5]
total = sum(numbers)
print(total) # Output: 15
Using a While Loop
numbers = [1, 2, 3, 4, 5]
total = 0
index = 0
while index < len(numbers):
total += numbers[index]
index += 1
print(total) # Output: 15
Using List Comprehension
numbers = [1, 2, 3, 4, 5]
total = sum([number for number in numbers])
print(total) # Output: 15
Using functools.reduce
from functools import reduce
numbers = [1, 2, 3, 4, 5]
total = reduce(lambda x, y: x + y, numbers)
print(total) # Output: 15
Using numpy Library
import numpy as np
numbers = np.array([1, 2, 3, 4, 5])
total = np.sum(numbers)
print(total) # Output: 15
Using math.fsum for Floating Point Precision
import math
numbers = [1.0, 2.0, 3.0, 4.0, 5.0]
total = math.fsum(numbers)
print(total) # Output: 15.0
Using itertools.accumulate for Cumulative Sums
import itertools
numbers = [1, 2, 3, 4, 5]
cumulative_sums = list(itertools.accumulate(numbers))
print(cumulative_sums) # Output: [1, 3, 6, 10, 15]
Using pandas Library
import pandas as pd
numbers = pd.Series([1, 2, 3, 4, 5])
total = numbers.sum()
print(total) # Output: 15
Using reduce with operator.add
from functools import reduce
import operator
numbers = [1, 2, 3, 4, 5]
total = reduce(operator.add, numbers)
print(total) # Output: 15
These methods provide various ways to sum elements in a list, depending on your specific needs and preferences.
Related Posts
For Looping in Python
Using for loops to manipulate arrays in Python.
pythonbackendarrays
While Looping in Python
Using while loops to manipulate arrays in Python.
pythonbackendarrays
Summary
Summary of the math library in Python.
pythonbackendarrays
Assignments
Assignments in Python for competitive programming.
pythonbackend