Using the .pop() function to take each element in a list and sum it up into a variable with (+=) but it cuts out after 4 elements

hello Im trying to solve the Carly’s Clipper project on the Computer science path. Link to project
but I’m having an issue with the second prompt, as It asks to iterate through a list and add or sum up each element and print the output into a variable. I decided to use a for loop and in the body of the loop create a variable called price and used .pop() to take each element in the variable and with a simple plus equals operation add each element taken from the list into the total variable. I will try to attach the prompt, and the code I am trying. My code is not raising any errors, but it is cutting off after just. a few elements.

" Iterate through the prices list and add each price to the variable total_price ."
the list

prices = [30, 25, 40, 20, 20, 35, 50, 35]

my code

total_price = 0

for price in prices:
  price = prices.pop()
  total_price += price
  print(total_price)

I hope I have the formatting right

Hello @lionsolano92, you could try using the range() and len() function.If you had a for loop like this:

for x in range(0, """number"""):
#code

This is because the loop iterates of the code the amount of times there are elements in the list.

Here is the working loop, but try not to look at it:
for x in range(0, (len(prices))):
  price = prices.pop()
  total_price += price
  print(total_price)

I hope this helps!

1 Like

pop() removes the element, and collapses the list. The one thing it offers us in terms of utility is the ability to remove and assign. We get to keep a copy of the value that was at that index.

It’s because of that feature that we are able to accumulate a total, but if we need to repeat the process, we cannot. The list is gone. Not to fear, though. We simply don’t iterate the prices list, and instead iterate a volatile copy which we are free to destroy. It’s virtual.

Iterate from the back end of a copy and none of the problems occur.

>>> prices = [30, 25, 40, 20, 20, 35, 50, 35]
>>> v_prices = prices[:]
>>> total = 0
>>> for price in v_prices[::-1]:
	total += v_prices.pop()

    
>>> total
255
>>> len(v_prices)
0
>>> prices
[30, 25, 40, 20, 20, 35, 50, 35]
>>> 
1 Like

@lionsolano92,

To add some context, the reason your original code does not work, but @tera5288723178’s does has to do with the nature of .pop(). If you look at the documentation for it (here), you will see that the .pop() method both removes the item and returns it. So what you are doing is iterating from left to right, while removing from right to left. So, after the first four iterations, you are at the end of your shortened list, as the other four numbers have already been removed and added to your total_price variable.

1 Like

ok this is weird, or maybe I did a typo or did not understood the website where I first read it from. But I tried to use the range(0, (len(price)): snippet and I got an error. Don’t remember which, but I tried it again today and it worked. Thanks. Sadly I had to look at your loop, cuz of the error I mentioned made me think that the Len() function would mess me up. But of course it did not, so maybe some other part of the loop I read before posting this was wrong. idk. Thank you though!!!

oh. So I have to provide the full range? I thought that it was cutting off for some reason, but my effort to give it a range was not working. Basically I used "price= [0:9] and other variations of that but I was getting an error message saying that my code was out of range.

Right, so the key to remember with .pop() is that your list will get shorter after each iteration. After you’ve used .pop() once your list will be one element shorter. So, if you are referencing the list itself for the number of times to loop through and .pop(), you will never loop the full number of elements.

The only way to loop through all the elements is to make sure you are looping through something else with the same number of elements as your original list. In @tera5288723178’s example, that thing is the range of 0 to the length of your list. In @mtf’s example, it’s a copy of your original list.

If you want to visualize it, it might look something like this with your original code:
(bold numbers are the index your loop is on, italic numbers are the numbers being removed with pop()

first iteration
[30, 25, 40, 20, 20, 35, 50, 35]
total_price = 35

second iteration
[30, 25, 40, 20, 20, 35, 50]
total_price = 85

third iteration
[30, 25, 40, 20, 20, 35]
total_price = 120

fourth iteration
[30, 25, 40, 20, 20]
total_price = 140

END OF LOOP
prices = [30, 25, 40, 20]
total_price = 140

1 Like

The reason it would have thrown an error is because price is not the name of the loop, and you can’t iterate through an int object. Thank you @mtf and @el_cocodrilo for expanding on my answer!

2 Likes