Comparing two arrays by index in python in a cleaner way I have a simple task I want to do, I want to compare two arrays like this:
array 1 of data:  [0.0, 92.6, 87.8, 668.8, 0.0, 86.3, 0.0, 147.1]
array 2 of data:  [16.7, 0.0, 0.0, 135.3, 0.0, 205.5, 0.0, 93.8]

I want to find the difference between each value by index, and if the difference is lower than 50%, keep the value of the second array in a third array.
array 3 of data : [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 93.8]

Now I believe using a for loop would be the best option, iterating through each array since both have the same length, the code below is part of my code, array1 and array2 are arrays like above.
for scan in sensor:
        #here is some code where we build the Array1 and Array 2 from serial data which is inside the loop.
    if specialcount ==1: #build the first array
      #get data from dict and build first array
      datadict={ 0:[], 1:[], 2:[], 3:[], 4:[], 5:[], 6:[], 7:[], }
    if specialcount ==2: #build the second array.
        #get data from dict and build second array
      datadict={ 0:[], 1:[], 2:[], 3:[], 4:[], 5:[], 6:[], 7:[], }
    if specialcount ==3: #after we build 2 arrays, specialcount goes to 3, and we now compare the arrays
            indexsizeofArray1 = len(array1)
            indexsizeofArray2 = len(array2)
            if indexsizeofArray1 != indexsizeofArray2:
                print (&quot;array mismatch, try again&quot;)
                break
            elif indexsizeofArray1 == indexsizeofArray2:
                print(&quot;array match, proccessing&quot;)
                array3 = [0,0,0,0,0,0,0,0]
                for X in range (indexsizeofArray2):
                    difference = array2[X]-array1[X]
                    fiftypercent = array1[X]-(array1[X]/2)
                    lowerrange = int(array1[X]-fiftypercent)
                    upperrange = int(array1[X]+fiftypercent)
                    if array2[X] in range (lowerrange,upperrange):
                        array3[X]=array2[X]
                    if X == indexsizeofArray2:
                        print(&quot;final array: &quot;,array3)
    specialcount +=1

Above is my code ,I do get the result I want of course, but I believe I could make it cleaner mostly because I am in the code transforming float to int as they work with the range function. I am sure there is a cleaner way of doing this , that specialcount==3 is my condition since I build the 2 arrays inside the forloop before as soon as the second array is built, then we, this is inside a loop where I am polling constantly a sensor, the sensor is special in the sense that if I stop polling it and go out of the loop, it will not work again unless I restart it , as such I must be inside this loop all the time.
I am sure I can improve the speed of it if slightly, mostly on the reinitialization of the datadict, since I initialized it before , however inside the loop I populate it with the data up until a certain point, in which I then create the first array of data, and clear it so I can rebuild it.  The goal is to build two arrays of 2 different variables that must be related, once those 2 lists  are built, I need to compare them, ensure that they are not that different and if they are not , pass it to another function , if its too different, the value for that index in the list is 0 .
Don't use the loop-switch antipattern
If you have three different things that have to be done in order, just write them in that order. Don't put a loop around them with if-statements inside to check what has to be done that iteration; loops are for doing the same task over and over. What you are doing has a name: it is the loop-switch antipattern. Instead, just write:
#get data from dict and build first array
datadict={ 0:[], 1:[], 2:[], 3:[], 4:[], 5:[], 6:[], 7:[], }

#get data from dict and build second array
datadict={ 0:[], 1:[], 2:[], 3:[], 4:[], 5:[], 6:[], 7:[], }

#we now compare the arrays
...

I assume that's not the real code. If each of the three tasks is of course much longer than what you wrote, then it helps to put them in functions.
Split the problem up into smaller sections
It always helps to break up a problem into smaller sections, and for example you can then handle each sub-problem in a separate function. In this case, looping over the arrays can be split from deciding whether or not to keep a value. So:
keep_within_fifty_percent(a, b):
    fiftypercent = a // 2
    lower = a - fiftypercent
    upper = a + fiftypercent

    if b in range(lower, upper):
        return b;
    else:
        return 0

...

#we now compare the arrays
if len(array1) != len(array2):
    # handle error here
    ...

array3 = [keep_within_fifty_percent(a, b) for a, b in zip(array1, array2)]
print(&quot;final array: &quot;, array3)