Loop over an an array of array

I have an array of arrays I want to loop over to return two arrays called hills and valleys. When looping through each element, we check the item at index 1 that is element[0] if the value is equal to zero, we create another array and push the value inside. we keep a trace of it still when we meet another element where the element[0] is greater than zero then the trace breaks after that we push what is in the array to the valley array and the same thing goes for hills.
Here is an example

arr = [[0.0, 1564.0], [0.0, 1565.0], [0.0, 1566.0], [0.0, 1567.0], [0.0, 1568.0], [0.0, 1569.0], [0.0, 1570.0], [7.18, 1571.0], [0.0, 1572.0], [0.0, 1573.0], [7.33, 1574.0], [0.0, 1575.0], [0.0, 1576.0], [7.18, 1577.0], [0.0, 1578.0], [0.0, 1579.0], [0.0, 1580.0], [0.0, 1581.0], [12.28, 1582.0], [0.0, 1583.0], [4.26, 1584.0], [0.0, 1585.0]]

should return hills [
[7.18, 1571.0],
[7.33, 1574.0],
[7.18, 1577.0],
[4.26, 1584.0]
] and

valleys should be [
[[0.0, 1564.0], [0.0, 1565.0], [0.0, 1566.0], [0.0, 1567.0], [0.0, 1568.0], [0.0, 1569.0], [0.0, 1570.0]],
[[0.0, 1572.0], [0.0, 1573.0]],
[[0.0, 1575.0], [0.0, 1576.0]],
[[0.0, 1578.0], [0.0, 1579.0], [0.0, 1580.0], [0.0, 1581.0]],
[[0.0, 1583.0]],
[[0.0, 1585.0]]
].

Here is what I have done

def plot_curve_with_annotation(input_array):

hills = []
valleys = []

for element in input_array:
value = element[0]

if value == 0.0 or value == None:
valleys.append(element)
elif value > 0.0:
hills.append(element)

return hills, valleys

hills, valleys = plot_curve_with_annotation(arr)
print("Hills:", len(hills))
print("Valleys:", len(valleys))

Does this look like it could work?

arr = [
    [0.0, 1564.0],
    [0.0, 1565.0],
    [0.0, 1566.0],
    [0.0, 1567.0],
    [0.0, 1568.0],
    [0.0, 1569.0],
    [0.0, 1570.0],
    [7.18, 1571.0],
    [0.0, 1572.0],
    [0.0, 1573.0],
    [7.33, 1574.0],
    [0.0, 1575.0],
    [0.0, 1576.0],
    [7.18, 1577.0],
    [0.0, 1578.0],
    [0.0, 1579.0],
    [0.0, 1580.0],
    [0.0, 1581.0],
    [12.28, 1582.0],
    [0.0, 1583.0],
    [4.26, 1584.0],
    [0.0, 1585.0]
]

hills = []
valleys = []

for element in arr:
    value = element[0]
    if value:
        hills.append(element)
    else:
        valleys.append(element)

Okay; so what problem are you actually encountering? What happens when you try using the code, and how is that different from the desired result?

What is the purpose of looping through the array at the beginning of the function entry? During every iteration, the value of the variable ‘value’ keeps getting overwritten with the current loop result. Thus, the way that the code is currently written, the value of the variable ‘value’, will ALWAYS be the value of the last element of the array, regardless of the values of all of the elements of the array.

The lack of correct indentation makes the code sample above rather non-trivial to naively follow (as well as syntactically invalid, of course), and is likely what is confusing you; with the correct indentation (which can be inferred both from the context and their explanation in the first few lines of the OP), it should become much more obvious that it is perfectly intentional value is overwritten each iteration, as it is never used outside the actual intended scope of the loop.

FWIW, in case it still isn’t clear, the following is what I presume are their intended indents (that outwardly appears to function as they intent):

def plot_curve_with_annotation(input_array):
    hills = []
    valleys = []
    
    for element in input_array:
        value = element[0]
        if value == 0.0 or value == None:
            valleys.append(element)
        elif value > 0.0:
            hills.append(element)
    
    return hills, valleys

arr = [[0.0, 1564.0], [0.0, 1565.0], [0.0, 1566.0], [0.0, 1567.0], [0.0, 1568.0], [0.0, 1569.0], [0.0, 1570.0], [7.18, 1571.0], [0.0, 1572.0], [0.0, 1573.0], [7.33, 1574.0], [0.0, 1575.0], [0.0, 1576.0], [7.18, 1577.0], [0.0, 1578.0], [0.0, 1579.0], [0.0, 1580.0], [0.0, 1581.0], [12.28, 1582.0], [0.0, 1583.0], [4.26, 1584.0], [0.0, 1585.0]]
hills, valleys = plot_curve_with_annotation(arr)
print("Hills:", len(hills))
print("Valleys:", len(valleys))

Thank you for the clarification. When I first seen his code, I thought it was a bug. It really helps making sure that the code is placed with the proper indentation.