Here’s the area of code I’m having problems with. T1 and T3 are both lists of lists:
for num in T1:
print num
print T3[0]
print type(num)
print type(T3[0])
if num == T3[0]:
print 'they are the same!'
else:
print 'nope they are not!'
if T3.index(num):
print 'number exists in list!'
Here’s the result:
[0, 0]
[0, 0]
<type 'list'>
<type 'list'>
they are the same!
The values and types are the same but when I don’t see ‘number exists in list!’ When I tested list_name.index(value) on the command line it works but not here. What am I doing wrong?
The method
list.index()returns the index of its argument in the list, and raises aValueErrorif the argument isn’t found. The checkbasically tests if the index of the argument is non-zero, but it happens to be zero in your example, so the check fails. You probably want to test if the item is contained in the list — use
to that end.