Skip to content Skip to sidebar Skip to footer

ValueError: Item Is Not In List

I am making this simple code: MyList=[] valueA=1 valueB=2 valueC=3 MyList.append (valueA) MyList.append (valueB) MyList.append (valueC) print (MyList) print ([MyList].index(valueB)

Solution 1:

[MyList] is a list consisting of a single item, which is MyList.

I don't know why you have wrapped MyList in another list. You need to call index on MyList itself:

print(MyList.index(valueB))

And the result will be 1, not 0, because valueB is the second item in MyList.


Solution 2:

Your mistake was that you put my_list into another anonymous list at the last line.It should be like this:

MyList=[]
valueA=1
valueB=2
valueC=3
MyList.append (valueA)
MyList.append (valueB)
MyList.append (valueC)
print (MyList)
print (MyList.index(valueB))

Output:

[1, 2, 3]
1

Post a Comment for "ValueError: Item Is Not In List"