I'm having trouble iterating through a dictionary's keys' values and performing mathematical operations where each key has a different number of values:
fruits = {
"banana": [4,5],
"apple": 2,
"orange":1.5,
"pear": 3
}
What I'd like from the code (i.e., my ideal production):
banana [8, 10]
apple 4
orange 3.0
pear 6
I want to times (*) each integer within each key by 2. I've tried the following but I am unable to get it right:
for fruit, price in fruits.items():
print(fruit, price*2)
for i in price:
print(fruit, i*2)
...But to no avail: this produces:
banana [4, 5, 4, 5]
banana 8
banana 10
apple 4
Okay, so I tried this:
for fruit, price in fruits.items():
#print(type(price))
if len(fruit)>0:
print(price*2)
elif len(fruit) == 0:
print(price*2)
To which it produced this:
[4, 5, 4, 5]
4
3.0
6
Is this even possible?
Any answers will be much appreciated.
Kind regards,
A. Smith