0
A = [5,2,9,-1,3,12]
def find_negative_one( A ):
    for num in range(len(A)):
        if A[num]== -1:
            return (A.index(A[num]))
indx_of_issue = find_negative_one( A )
print(indx_of_issue)

The feedback was that my loop is redundant, pls how can i make it none redundant

jamie
  • 19
  • 2
  • 1
    `A.index(-1)` loop is not required. – sittsering Oct 16 '21 at 13:34
  • Have you checked the documentation. [`list.index(value)`](https://docs.python.org/3/tutorial/datastructures.html#more-on-lists). You just have to write `A.index(-1)`. From your code `return(A.index(A[num])` is calling `A.index(-1)` if -1 exists. – Ch3steR Oct 16 '21 at 13:36

1 Answers1

1

Since you are only returning the index of 1st occurrence of -1, you don't need to use loop.

def find_negative_one(A):
    try:
        return A.index(-1) # throws ValueError if not found
    except ValueError:
        return -1

or

def find_negative_one(A):
    if -1 in A: # checks if -1 is present in the list
        return A.index(-1)
    else:
        return -1
sittsering
  • 1,219
  • 2
  • 6
  • 13