I am new to python so maybe my problem is easy to solve. I have a dictionary organized as follow: my_dict = {'x': (1.1, 3.4), 'y': (4.3, 7.5), 'z': (7.3, 9.5)}. I would like to reduce each value by the same number, so this is what I am trying to do:
for k,v in my_dict.items():
for a, b in v:
a = a * 0.3
b = b * 0.3
my_dict[k] = (a, b)
return my_dict
When using this code, I receive this error:
for a, b in v:
TypeError: 'float' object is not iterable
So it seems that the "for a, b in v" loop is trying to iterate through float, which is not possible. Thus, I tried to print my_dict.items() and k,v in the for loop, in order to see what is the for loop iterate through, and this is what I get:
for k,v in my_dict.items():
print my_dict.items()
print k,v
[('x', (1.1, 3.4)), ('y', (4.3, 7.5)), ('z', (7.3, 9.5))]
y (4.3, 7.5)
I can see two wierd things: - I was expecting the "print k,v" command to print each key, values couple, but I am getting only the y key with its value - By looking at the my_dict.items(), it seems to me that v is not a float, but a tuple containing two float. So why do I get the float object is not iterable error?
Any help is well appreciated. Again, I am sorry if this is a silly question, but I just started using python and I am stuck with this.
Thank you!
a, b = vto unpack the tuple