I have a one-dimensional array containing values and I'm trying to use a for loop to identify the index values associated with non-zero elements.
For the code below, non_zero_elements should contain the values 0, 1, and 4, but what I am getting is [1, 0, 0, 4, 0].
I tried referencing a similar thread (Finding Non-Zero Values/Indexes in Numpy) but couldn't identify the bug in my code.
a = [1,2,0,0,4,0]
non_zero_elements = []
i = 0
for i in a:
if a[i] != 0:
non_zero_elements.append(i)
print('The value',a[i],'in index',i,'is a non-zero element.')
i = i + 1
print('Non-zero elements: ',non_zero_elements)