close

[Solved] Python index error value not in list…on .index(value)

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error Python index error value not in list…on .index(value) in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How Python index error value not in list…on .index(value) Error Occurs?

Today I get the following error Python index error value not in list…on .index(value) in python.

How To Solve Python index error value not in list…on .index(value) Error ?

  1. How To Solve Python index error value not in list…on .index(value) Error ?

    To Solve Python index error value not in list…on .index(value) Error subset.index(currentPosition) evaluates False when currentPosition is at index 0 of subset, so your if condition fails in that case. What you want is probably:

  2. Python index error value not in list…on .index(value)

    To Solve Python index error value not in list…on .index(value) Error subset.index(currentPosition) evaluates False when currentPosition is at index 0 of subset, so your if condition fails in that case. What you want is probably:

Solution 1

Let’s show some equivalent code that throws the same error.

a = [[1,2],[3,4]]
b = [[2,3],[4,5]]

# Works correctly, returns 0
a.index([1,2])

# Throws error because list does not contain it
b.index([1,2])

If all you need to know is whether something is contained in a list, use the keyword in like this.

if [1,2] in a:
    pass

Alternatively, if you need the exact position but don’t know if the list contains it, you can catch the error so your program does not crash.

index = None

try:
    index = b.index([0,3])
except ValueError:
    print("List does not contain value")

Solution 2

subset.index(currentPosition) evaluates False when currentPosition is at index 0 of subset, so your if condition fails in that case. What you want is probably:

...
if currentVal == 0 and currentPosition in subset:
...

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read