prices[item] * cart[item] for item in cart is a generator. It produces an iterable, which is what the sum function takes as its first parameter.

for item in cart iterates over each of the keys in the cart dictionary. So in this case, it will iterate over the list ['apple', 'egg'], setting item to each of these in turn. For each iteration, the expression prices[item] * cart[item] looks up the price of an item (prices['apple'] for example) and multiplies it by the quantity of an item (cart['apple'] for example). The generator produces a sequence consisting of the result of computing the value of this expression for each key in cart, and then sum takes that sequence and adds the elements of it together to come up with the final total of the items in cart.

Answer from CryptoFool on Stack Overflow
🌐
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!

🌐
University of Vermont
uvm.edu › ~cbcafier › cs1210 › book › 11_loops › loops_and_summation.html
Math and Python: loops and summation – Clayton Cafiero
While we have the Python built-in function sum() which sums the elements of a sequence (provided the elements of the sequence are all of numeric type), it’s instructive to see how we can do this in a loop (in fact, summing in a loop is exactly what the sum() function does). Here’s a simple example. t = (27, 3, 19, 43, 11, 9, 31, 36, 75, 2) sum_ = 0 for n in t: sum_ += n print(sum_) # prints 256 assert sum_ == sum(t) # verify answer
Discussions

Python - SUM(Iterator, FOR Loop) - How does this work? - Stack Overflow
I am continuing my learning journey on Python and came across a snippet of code that I am quite confused as to how it works regarding the SUM() function in Python. The code is as follows prices = {... More on stackoverflow.com
🌐 stackoverflow.com
python - Get sum of numbers using a Function and For Loop - Stack Overflow
The function sumAll needs to use a for loop to carry out this summation, and it will have to use a sum variable that increases in value over each iteration of the for loop. More on stackoverflow.com
🌐 stackoverflow.com
How does sum function work in python with for loop - Stack Overflow
I was using sum function in python, and i am clear about it's general structure sum(iterable, start) , but i am unable to get the logic behind the following code test = sum(5 for i in range(5) ) p... More on stackoverflow.com
🌐 stackoverflow.com
December 31, 2018
Python: Calculating a sum for each loop - Stack Overflow
I need to find how to calculate the sum of the indefinite numbers per loop, the problem i'm having right now is that the loop is not resetting each value for each person and i'm getting a sum total. More on stackoverflow.com
🌐 stackoverflow.com
🌐
GeeksforGeeks
geeksforgeeks.org › python › sum-function-python
sum() function in Python - GeeksforGeeks
November 27, 2025 - A for loop can be used to add elements of a list one by one. This method shows how summation works step-by-step and is useful when you need full control over how each value is processed during the calculation.
🌐
Bobby Hadz
bobbyhadz.com › blog › python-sum-in-for-loop
How to sum in a For or a While Loop in Python | bobbyhadz
The range class is commonly used for looping a specific number of times in for loops and takes the following parameters: If you need to sum numbers taken from user input in a for loop, use the input() function.
🌐
Real Python
realpython.com › python-sum-function
Python's sum(): The Pythonic Way to Sum Values – Real Python
March 18, 2026 - In the second for loop, the function iterates over each item in sublist to finally populate the new flat list with .append(). Arguably, readability can be an advantage of this solution. As you’ve already learned, sum() is helpful for working with numeric values in general. However, when it comes to working with floating-point numbers, Python provides an alternative tool.
🌐
Great Learning
mygreatlearning.com › blog › python exercises › python loop program to sum numbers
Python Loop Program to Sum Numbers
December 17, 2025 - # 1. Initialize the accumulator total_sum = 0 # 2. Loop from 1 to 100 (range stops before 101) for i in range(1, 101): total_sum = total_sum + i # 3.
Find elsewhere
🌐
EyeHunts
tutorial.eyehunts.com › home › sum of n numbers in python using for loop | example code
Sum of n numbers in Python using for loop | Example code
October 4, 2023 - We use a for loop to iterate from 1 to n, adding each number to the sum_of_numbers variable. Finally, we print out the sum. You can run this code by copying and pasting it into a Python interpreter or a script file, and it will calculate the ...
🌐
Python Guides
pythonguides.com › sum-elements-in-list-in-python-using-for-loop
How to Sum Elements in a List in Python
November 10, 2025 - The most obvious way to add numbers stored in a list is to use a for-loop. The following code fragment visits each list element in order and totals the sum of its elements.
🌐
PYnative
pynative.com › home › python › python program to calculate sum and average of first n natural numbers
Python program to calculate sum and average of first n natural numbers
June 16, 2021 - In each iteration, add the current value of n to the sum variable and decrement n by 1. Calculates the average by dividing the sum by n (total numbers). n = 20 total_numbers = n sum = 0 while n >= 0: sum += n n -= 1 print("sum =", sum) # Output ...
🌐
Python.org
discuss.python.org › python help
Python summing in loop vs sum() performance - Python Help - Discussions on Python.org
April 3, 2023 - Hey everyone! Recently I was playing with python. And noticed in my opinion strange behaviour. I have 4 functions: def t1(): s = time.perf_counter_ns() counter = 0 for _ in range(10_000_000): counter += 1 e = time.perf_counter_ns() print(counter) return int((e - s) / 1_000_000) def t2(): s = time.perf_counter_ns() counter = sum(1 for _ in range(10_000_000) e = time.perf_counter_ns() print(counter) return int((e - s) / 1_000_000) def t11():...
🌐
Sololearn
sololearn.com › en › Discuss › 2922107 › given-a-list-of-numbers-calculate-their-sum-using-a-for-loop-output-the-sum-after-the-loop
Given a list of numbers, calculate their sum using a for loop ...
sl_scroll_/de/Discuss/1943908/essential-programming-languages-for-electrical-engineering-studentsPending · Maximum Storage Duration: SessionType: HTML Local Storage · sl_scroll_/de/Discuss/1953300/python-artificial-intelligence-Pending
🌐
Engineering LibreTexts
eng.libretexts.org › campus bookshelves › delta college › introduction to programming concepts - python › 5: iterations
5.8: Counting and Summing Loops - Engineering LibreTexts
July 27, 2020 - As the loop executes, total accumulates the sum of the elements; a variable used this way is sometimes called an accumulator.
🌐
Stack Overflow
stackoverflow.com › questions › 39316646 › how-to-do-sum-in-loop
python - How to do sum in Loop - Stack Overflow
with your current code what you get is the total sum of all the given numbers, to get the sum per line you need to initialize your counter in the outer loop, and then print it, and as you want to print all it in the same line there are several ...