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 = {'apple': 0.75, 'egg': 0.50}
cart = {
'apple': 1,
'egg': 6
}
bill = sum(prices[item] * cart[item]
for item in cart)
print(f'I have to pay {bill:.2f}')
The final output of this is "I have to pay 3.75"
The part that really confuses me is in the SUM function with the "iterator" or the "for item in cart"
From python documentation on the SUM function it states
sum(iterable, [start])
Iterable: Item like string, list, dictionary etc.
Start: An optional numeric value added to the final result. It defaults to 0.
So for example if with this code
sum([1,2,3], 4)
This would basically work out to 1+2+3+4=10, which makes sense to me.
So I am confused how the "for loop" portion of the snippet of code is legal?
I tried googling around but most examples I find are pretty simple like the one I just mentioned, and I cant find any explanations on how the FOR loop works with SUM like this