FongYu
FongYu

Reputation: 767

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

I am a beginner at Python, and to those who holds negative thoughts against my post, please leave. I am simply seeking help here and trying to learn. I'm trying to check within a simple data set the 0s and 1s. This will be used towards defining voids and solids on floor plans to define zones in buildings... eventually 0s and 1s will be swapped out with coordinates.

I am getting this error: ValueError: [0, 3] is not in list

I am simply checking if one list is contained in the other.

currentPosition's value is  [0, 3]
subset, [[0, 3], [0, 4], [0, 5], [1, 3], [1, 4], [1, 5], [2, 1], [3, 1], [3, 4], [3, 5], [3, 6], [3, 7]]

Here's the code snippet:

def addRelationship(locale, subset):
    subset = []; subSetCount = 0
    for rowCount in range(0, len(locale)):
        for columnCount in range (0, int(len(locale[rowCount])-1)):
            height = len(locale)
            width = int(len(locale[rowCount]))
            currentPosition = [rowCount, columnCount]
            currentVal = locale[rowCount][columnCount]
            print "Current position is:" , currentPosition, "=", currentVal

            if (currentVal==0 and subset.index(currentPosition)):
                subset.append([rowCount,columnCount])
                posToCheck = [rowCount, columnCount]
                print "*********************************************Val 0 detected, sending coordinate to check : ", posToCheck

                newPosForward = checkForward(posToCheck)
                newPosBackward = checkBackward(posToCheck)
                newPosUp = checkUpRow(posToCheck)
                newPosDown = checkDwnRow(posToCheck)

I am using subset.index(currentPosition) to check and see if [0,3] is in subset but getting the [0,3] is not in list. How come?

Upvotes: 18

Views: 118230

Answers (4)

Tobitibi
Tobitibi

Reputation: 1

I found out that

list = [1,2,3]

for item in range(len(list)):
    print(item)

won't work because it starts at 0, so you need to write

for item in range(1, len(list)):
    print(item)

Upvotes: 0

Abhishek
Abhishek

Reputation: 3417

Why complicate things

a = [[1,2],[3,4]]
val1 = [3,4]
val2 = [2,5]

check this

a.index(val1) if val1 in a else -1
a.index(val2) if val2 in a else -1

Upvotes: 3

Silas Ray
Silas Ray

Reputation: 26150

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:
...

Upvotes: 1

danmcardle
danmcardle

Reputation: 2109

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")

Upvotes: 30

Related Questions