You have 2 ways to access a value in a Tuple:
- By index:
shares = block[2]
- By unpacking all the values:
purchase_date, purchase_price, shares, symbol, price = block
Given this two ways, here are two possible solutions to your problem
def total_purchase_price(stocks):
res = []
for block in stocks:
shares = block[2]
purchase_price = block[1]
res += [shares * purchase_price]
return res
or
def total_wins(stocks):
win = 0
for block in stocks:
purchase_date, purchase_price, shares, symbol, current_price = block
win += (purchase_price - current_price) * shares
return win
You would call these functions in this manner:
portfolio= [ ( "25-Jan-2001", 43.50, 25, 'CAT', 92.45 ),
( "25-Jan-2001", 42.80, 50, 'DD', 51.19 ),
( "25-Jan-2001", 42.10, 75, 'EK', 34.87 ),
( "25-Jan-2001", 37.58, 100, 'GM', 37.58 )
]
print total_purchase_price(portfolio)
print total_wins(portfolio)