I am trying to write a script in python to solve the following issue:
when I plot some angle trajectory, I get jumps due to the fact that the point I am tracking sometimes it 'jumps' outside the range [0, 360]. 
Given that these jumps are fairly easy to identify (see plot in the link at the end of the post), I was trying to write a function in python that takes as input the list of angles, and when it identifies jumps >300 it will subtract 360 from the following values of the list until it finds the second jump.
Here is my attempt to code this, but unfortunately I am running an infinite loop. Does anyone have any suggestions?
EDIT: I added a list with some of the values just as an example
alpha = [356.37, 359.80, 357.14, 359.18, 350.97, 347.35, 348.98, 351.80,  2.74, 354.55, 354.13, 357.82,  3.86, 2.42, 3.57, 1.57, 357, 358]
#take derivative of list of angles alpha
alpha_diff = [0, np.diff(alpha)]
#iterate over each item in alpha_diff and when it finds jumps <-360 you will add 360 until it finds the next jump
for i in alpha_diff:
    if alpha_diff[i]<-300:
        while alpha_diff[i]<100:
            alpha[i] = alpha[i]+360
#similarly if finds jumps > 360 you will subtract 360 until it finds the next jump
    if alpha_diff[i]>300:
        while alpha_diff[i]<100:
            alpha[i] = alpha[i]-360 
    else:
        alpha[i] = alpha

 
     
    

