Iterating Through Float Dictionary

Hello! I am attempting to run a differential equation and am experiencing some issues when iterating over my dictionary, which contains float values. Ultimately, I want to add each key and value in the equation contained within the for loop. Please let me know if you have any ideas of how to solve this!

# membrane potential, Vm, as a function of time (for Vth = 0.5)

def Vm_over_time(I_half_dict, t):
# *** throws TypeError: 'float' object is not iterable ****
   for i_in,i_out in I_half_dict.items(): 
     dVmdt = -1*(i_in + i_out)/100
     return dVmdt 

# dictionary of key:value pairs corresponding to I_in_half:I_out_half 
I_half_dict = dict(zip(I_in_half, I_out_half))

# time points
t = np.linspace(0,20,1000) 

# solve ODE 
Vm = odeint(Vm_over_time, -0.80, t)

I may be misinterpreting the type of some of your values (a minimal working example might be useful) but is that actually where the error is thrown? What’s the actual traceback you get?

Did you intend for that return to be inside the loop? Doesn’t look right.

This is a bit of a stab in the dark since I’ve not done much in graphing, but isn’t this running
Vm_over_time(-0.80, t)
instead of putting a the I_half_dict into the function? So you end up trying to iterate on -0.80.items()

taylorrayne shared this in a previous post: Google Colaboratory

1 Like

@bavarcarus wins Detective of the Year award :male_detective:

That is exactly what is happening.

See scipy.integrate.odeint — SciPy v1.7.0 Manual for more context here.

1 Like

Ah, return always trips me up. I am thinking that I would want to return the derivative at each point/value in order to plot the function. But correct me if my understanding of what return is doing relative to the loop is incorrect!

1 Like

Thanks everyone for jumping on to help!
@bavarcarus @mtf @el_cocodrilo @tgrtim

2 Likes

It’s exiting the function on the first iteration. Perhaps build a list, return that and then plot it.

oh, great idea. Thanks!

1 Like