0

I'm trying to sum up a list of numbers from the list but for some reason it just kept showing TypeError, can somebody tell me why and how I can fix it?

portfolio = [('AAPL',5, 140),('FB',3, 330)]

for i in portfolio:
    sum_value = 0
    stocks  = list (i)
    item_value = stocks[1] * stocks[2]
    print(sum(item_value))



---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-56-5825164afaac> in <module>
      5     stocks  = list (i)
      6     item_value = stocks[1] * stocks[2]
----> 7     print(sum(item_value))
      8 
      9 

TypeError: 'int' object is not iterable
    
4
  • 1
    item_value is an integer, you can not use the sum() function on only one integer. Could you show the expected output? Commented Oct 13, 2021 at 14:41
  • item_value contains the result of stocks[1] * stocks[2]. So it's just an integer. Commented Oct 13, 2021 at 14:42
  • The expected output for the codes should be like this, [in]: portfolio_value([('AAPL',5, 140),('FB',3, 330)]) [out]: 1690 Commented Oct 13, 2021 at 14:48
  • The expected output for the codes should be: when the function takes inputs for both portfolios, the output should be the sum of each portfolio value, for AAPL, it would be 700, FB would be 990 and the expected output would be sum of both numbers which is 1690 Commented Oct 13, 2021 at 15:26

4 Answers 4

2

sum expects a iterable object, like a list, while you are passing just a single value.

you can solve this with:

sum_value = 0
for i in portfolio:
    stocks  = list (i) #if i is a tuple there is no need for this conversion
    sum_value += stocks[1] * stocks[2]
print(sum_value)

or in a single line:

sum([item[1]*item[2] for item in portfolio])
Sign up to request clarification or add additional context in comments.

1 Comment

Tuples are indexed, so they can be accessed similar to lists. So there is no need to convert it into list for stocks in portfolio: sum_value += stocks[1] * stocks[2] this should make this even simple.
1

You can use generator expressions and pattern matching on tuples to simplify the code

print(sum(item_value*stock_value for (_,item_value,stock_value) in portfolio))

1 Comment

@Sayse that's generator expression and pattern matching on tuples. You're right!
0

Do you mean something like this?

portfolio = [('AAPL', 5, 140), ('FB', 3, 330)]

item_sum = 0

for i in portfolio:
    sum_value = 0
    stocks = list(i)
    item_value = stocks[1] * stocks[2]
    item_sum += item_value
print(item_sum)

In your code, item_value is an int but not a list, it can't be in sum() What you need to do is declare a variable to save and add value to it.

1 Comment

I tried this method but it only returned the sum of each single stock (700 and 990), they still wouldn't add up tot he sum.
-1

the error is because you are passing a int value into the function sum you must pass a list like this

sum([3,4])

and the result is 7

you should use something like this

portfolio = [('AAPL',5, 140),('FB',3, 330)]

for i in portfolio:
    sum_value = 0
    stocks  = list (i)
    item_value = stocks[1] * stocks[2]
    print(sum([sum_value,item_value]))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.