For Loop In a Dictionary

Hello guys,

Please, how can I use a For Loop with Dictionary for this problem?

This is what I have struggled with but getting three times the result. I want the result to
lists all 10 stock prices for each stock. I also want to use a for loop to print out the stocks minimum price, average price, and maximum price for each stock. I can do this with just a list, but got stuck with this.

import statistics

stock_prices = {'BTC':  [22000, 24000, 19000, 25000, 33000, 40000, 41000, 38000, 35000, 27000],
        'BNB': [227,  222, 230, 240, 221, 250, 251, 200, 216, 218],
        'SOL':[30, 31, 28, 33, 40, 42, 39, 36, 37, 38]
        }

for key, value_list in stock_prices.items():
    
    print(stock_prices)

Try this:

for key, value_list in stock_prices.items():
    print(key)
    print("Values:", value_list)
    print('Average (median)', statistics.median(value_list))
    print('Average (mean)', statistics.median(value_list))
    print('Min and max price', min(value_list), max(value_list))
1 Like

Hi Eagle Eye. It sound like you’re wanting to…

for key, value_list in stock_prices.items():
    print(key, value_list)

…with some min(), average(), and max() processing on each loop cycle.

If so, then you only need to loop through the dictionary items. You don’t need to loop through the item values because the min() etc. functions will scan through the value elements.

Would you like to take a shot at revising your code using the points above or would you like some assistance with the changes?

1 Like

Thanks Steven. That worked !

Thanks Leland, that also worked. !