-2

I'm trying to write a Pyhon function that checks whether a list contains a given index number.

def ncheck(n, list):
    for x in range(len(list)):
        if n == x:
            return True
        else:
            return False

I tested the code with the following arguments:

print(ncheck(6, [0, 1, 2, 3]))
print(ncheck(2, [0, 1, 2, 3]))

I expected the first run to return False since it's looking for index 6 on a list with 4 elements and the second run to return True because it's looking for index 2. What actually happens is both runs return False. I thought that maybe having return inside the if block caused the problem, so I tried assigning another variable "answer" inside the if block and returning this variable as the closing statement for the function but got the same result:

def ncheck(n, list):
    for x in range(len(list)):
        if n == x:
            answer = True
        else:
            answer = False
    return answer

print(ncheck(6, [0, 1, 2, 3]))
print(ncheck(2, [0, 1, 2, 3]))

False
False

I'm at a loss. More than an alternate way to solve this issue, I want to understand why this method specifically isn't working.

hylea
  • 3
  • 3
  • ```list``` in python is a built in function so now you shadow it inside your function so use a different name. – KMG Jun 24 '21 at 23:25
  • 1
    But why not just `return n < len(list)`? (putting aside `list` being a bad variable name as mentioned by @Khaled) or even better, just catching an `IndexError` wherever you intend to call this function? – DeepSpace Jun 24 '21 at 23:27
  • You should have used a debugger before asking the question here. Learn about pdb and debugging in general. – amritkrs Jun 25 '21 at 05:52

1 Answers1

0

Your code currently just checks the first number, your else is intended to run if all numbers don't match:

def ncheck(n, list):
    for x in range(len(list)):
        if n == x:
            return True
    return False # after exiting the for loop

print(ncheck(6, [0, 1, 2, 3]))
print(ncheck(2, [0, 1, 2, 3]))

Returns

False
True

Additionally, note that range is actually capable of doing this as an O(1) operation, so the preferred way of doing this (independent of the length of list) is:

def ncheck(n, list):
    return n in range(len(list))

You can see this answer for more details.

Kraigolas
  • 5,121
  • 3
  • 12
  • 37
  • 2
    or just `return n < len(list)` – DeepSpace Jun 24 '21 at 23:29
  • 1
    using last method will just work if array is actually just increments of 1 and will fail if arrays is not this number sequence. – KMG Jun 24 '21 at 23:31
  • @DeepSpace The example in the question checks the range, which means that in this case, negative values of `n` are not desired. – Kraigolas Jun 24 '21 at 23:31
  • @Khaled I interpret the question as "how can I check if `n` is a valid, non-negative index into my list". You can see from OP's example that they use `range` as well because they are not trying to compare to the elements in the list, so the elements in the list should be irrelevant. – Kraigolas Jun 24 '21 at 23:34
  • @Kraigolas OP is merely trying to compare `n` to the **length** of the list, which, by definition, can not be negative, so I'm not sure what negative numbers have to do with this. If you really want to guard against getting `True` for negative *inputs*, just have `return 0 <= n < len(list)`. There is really no need for a confusing loop here – DeepSpace Jun 24 '21 at 23:37