First, don't overwrite list and sum, those are already existing functions. Second I am trying to take 1, and then add 4 Not exactly. You don't take 1 and then add 4. You take 0 and then add 1, then add 4, etc. So what you've forgotten is to "initialize" your sum as 0. Then you iteratively add all the values to that. l = [1, 4, 6, 3, 7, 5, 2, 8, 9] cumsum = 0 for i in l: cumsum += i If you don't first initialize a variable to 0, you have nothing to add that first value to. That's the piece you were missing. Edit: Minor improvement for clarity. Answer from synthphreak on reddit.com
๐ŸŒ
FACE Prep
faceprep.in โ€บ home โ€บ articles โ€บ sum of array elements in python: program to add array elements | face prep
Sum of Array Elements in Python: Add Array Elements | FACE Prep
March 3, 2025 - Use a for loop to collect array elements from the user. Append each element to the list. Use Pythonโ€™s built-in sum() function to calculate the sum of all elements in the list.
๐ŸŒ
Django Central
djangocentral.com โ€บ sum-of-elements-of-an-array-or-list
Python Program To Find The Sum Of Elements Of An Array or List
Make a Python Function which takes ... to store the sum and initializing it to 0. Next, the for loop iterates over each element of the array / list and storing their sum in the total variable, at last function returns the total....
Discussions

How to cumulative sum over for loop?
First, don't overwrite list and sum, those are already existing functions. Second I am trying to take 1, and then add 4 Not exactly. You don't take 1 and then add 4. You take 0 and then add 1, then add 4, etc. So what you've forgotten is to "initialize" your sum as 0. Then you iteratively add all the values to that. l = [1, 4, 6, 3, 7, 5, 2, 8, 9] cumsum = 0 for i in l: cumsum += i If you don't first initialize a variable to 0, you have nothing to add that first value to. That's the piece you were missing. Edit: Minor improvement for clarity. More on reddit.com
๐ŸŒ r/learnpython
20
12
December 17, 2021
Finding the sum of an array in python using a loop?
total = rannum = rannum What exactly do you expect this line to do? More on reddit.com
๐ŸŒ r/learnprogramming
16
2
February 10, 2021
python - Get sum of numbers using a Function and For Loop - Stack Overflow
Communities for your favorite technologies. Explore all Collectives ยท Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work More on stackoverflow.com
๐ŸŒ stackoverflow.com
Summing up elements in a list in python using a for loop - Stack Overflow
While working on a program that creates bars out of sets of numbers in a list, I found that adding up items in my list doesn't work. I thought the best way to do this is just to make a for loop. H... More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
University of Vermont
uvm.edu โ€บ ~cbcafier โ€บ cs1210 โ€บ book โ€บ 11_loops โ€บ loops_and_summation.html
Math and Python: loops and summation โ€“ Clayton Cafiero
We begin with a tuple of numeric values, t. Since the elements of t are all numeric, we can calculate their sum. First, we create a variable to hold the result of the sum. We call this, sum_.1 Then, we iterate over all the elements in t, and at each iteration of the loop, we add the value of ...
๐ŸŒ
DEV Community
dev.to โ€บ askyt โ€บ python-program-to-find-the-sum-of-an-array-bci
Python Program to Find the Sum of an Array - DEV Community
January 31, 2025 - # Function to calculate sum using a loop def sum_array(arr): total = 0 for num in arr: total += num return total # Example usage arr = [5, 8, 12, 20] print("Sum of the array is", sum_array(arr))
๐ŸŒ
PrepBytes
prepbytes.com โ€บ home โ€บ python โ€บ python program to find sum of array
Python Program to Find Sum of Array
June 18, 2024 - Q3: How can you find the sum of elements in an array? You can find the sum of elements in an array using a loop to iterate through the elements and accumulate the sum or by using Pythonโ€™s built-in sum() function.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-program-to-find-sum-of-array
Python Program to Find Sum of Array - GeeksforGeeks
November 4, 2025 - Explanation: reduce() applies a function cumulatively to items of the iterable, combining them into a single result (here it repeatedly adds pairs). Iterating through the array and adding each element to the sum variable and finally displaying ...
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ how to cumulative sum over for loop?
r/learnpython on Reddit: How to cumulative sum over for loop?
December 17, 2021 -

Hello, so I am trying to understand how to sum values through a for loop within python so that I start with a value, and then add to it, and then add the next value to that, and then add the next value to that, and so on, going through all values in my list/folder. I think this would be referred to as a "cumulative sum". So let's say I have a list of numbers:

list = [1,4,6,3,7,5,2,8,9]

I want to iterate over each value in this list and add all values to the previous like so:

for value in list:
    1 + value
    sum = 1 + value
    print(sum)

or something like that? I tried this, and it just adds one to each value in the list, when I want a single final cumulative sum produced. I am really confused about if my logic is correct there. Is this how I would carry out this cumulative sum operation? I am trying to take 1, and then add 4, and then add 6 to that, and then add 3 to that sum, and then add 7 to that sum, and so on. I realize one could also just add all of these values at once, but I am trying to understand how to do this with a for loop. Thanks!

Find elsewhere
๐ŸŒ
Tutorial Gateway
tutorialgateway.org โ€บ python-program-to-find-sum-of-numpy-array
Python Program to Find Sum of Numpy Array
January 24, 2025 - Within this for loop, we are adding each item to the total (total = total + sumArr[I]). import numpy as np sumArr = np.array([10, 60, 30, 40, 70, 95]) total = 0 for i in range(len(sumArr)): total = total + sumArr[i] print("The Sum of Total Array ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ sum-function-python
sum() function in Python - GeeksforGeeks
January 2, 2025 - ... In this, the code first defines a list of numbers. It then initializes a variable called total to 0. The code then iterates through the list using a for loop, and for each number in the list, it adds that number to the total variable.
๐ŸŒ
CodeRivers
coderivers.org โ€บ blog โ€บ array-sum-python
Mastering Array Sum in Python: A Comprehensive Guide - CodeRivers
February 22, 2026 - Here's how it works: my_list = [1, 2, 3, 4, 5] sum_value = 0 for num in my_list: sum_value += num print(sum_value) In this code, we initialize a variable sum_value to 0. Then, we iterate through each element in the my_list using a for loop. For each element, we add it to the sum_value.
๐ŸŒ
w3resource
w3resource.com โ€บ python-exercises โ€บ numpy โ€บ numpy-cumulative-sum-of-large-array-using-for-loop-and-optimization.php
NumPy - Cumulative sum of large array using For loop and optimization
September 1, 2025 - Learn how to calculate the cumulative sum of a large 1D NumPy array using a for loop and optimize it with NumPy's cumsum() function. Follow our step-by-step guide for efficient array operations.
๐ŸŒ
Bobby Hadz
bobbyhadz.com โ€บ blog โ€บ python-sum-in-for-loop
How to sum in a For or a While Loop in Python | bobbyhadz
The map() function passes each string to the int() class and converts it to an integer. ... Iterate for as long as the number is greater than 0. On each iteration, decrement the number by 1. On each iteration, increment the total by the number.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnprogramming โ€บ finding the sum of an array in python using a loop?
r/learnprogramming on Reddit: Finding the sum of an array in python using a loop?
February 10, 2021 -

Why am i not finding the total in the list?

ranger = [15,30,45,50, 51,55,57,58]

for rannum in ranger: total = rannum +rannum

print(total)

Top answer
1 of 2
2
total = rannum = rannum What exactly do you expect this line to do?
2 of 2
1
OK! I think you understand the logic behind what you are trying to do, but are a bit confused about translating it into your code. Let's dissect what you have currently: ranger = [15,30,45,50, 51,55,57,58] for rannum in ranger: total = rannum +rannum print(total) So you create an array with a list of integers. That's all good. Then you use "for rannum in ranger:" to iterate through your list, which is also good. This is where you lose it. Inside of the for loop you declare a variable named total which is equal to rannum + rannum. If the total variable is declared within the scope of the for loop, it get redeclared on every loop. So the first thing we need to do is move the total variable outside of the for loop: total = 0 for rannum in ranger: total = rannum +rannum So we now need to talk about what the for loop is doing: it executes the code that is inside the for loop one time for each item in the array ranger, assigning one value at a time to rannum. So the first time it loops, rannum equals 15, then the second time rannum equals 30, then 45, and so on. What we want is to save a running total of these values into the variable total. What you are doing currently is adding the current value of rannum (which is different every loop) to itself, then assigning that value to total, meaning total gets completely overwritten every time. Instead, we want to INCREMENT the value of total using the current value of rannum like this: total = 0 for rannum in ranger: total += rannum This way, as the for loop loops through all your values in ranger, it adds that value to the current total stored in the variable total. Let me know if that doesn't make sense and I can break it down more.
๐ŸŒ
Python Guides
pythonguides.com โ€บ sum-elements-in-list-in-python-using-for-loop
How To Sum Elements In A List In Python
June 3, 2024 - As we have seen, this can be done in many different ways, depending on the situation and aesthetic preferences. Be it through a very simple for loop, using the sum() function, the perfect, concise way of using list comprehensions, or the more ...
๐ŸŒ
PREP INSTA
prepinsta.com โ€บ home โ€บ python program โ€บ calculate the sum of elements in an array using python
Sum of Elements in an array using Python | PrepInsta
October 6, 2022 - import numpy as np n=int(input()) s=0 a=np.array([input().split() for i in range(n)] ,int) print(sum(a)) ... n = int(input(โ€œEnter the size of the array. \nโ€)) arr = [] for i in range(n): element = int(input()) arr.append(element) arr.sort() print(fโ€The array are: {arr} \nโ€) print(fโ€The sum of the total array are: {sum(arr)}โ€)...
๐ŸŒ
Studytonight
studytonight.com โ€บ python-programs โ€บ python-program-to-find-sum-of-array
Python Program to Find Sum of Array - Studytonight
July 2, 2021 - ... Look at the program to understand the implementation of the above-mentioned approach. import array as ar def SumofArray(arr): sum=0 n = len(arr) for i in range(n): sum = sum + arr[i] return sum #input values to list a = ar.array('i',[10, ...