2

I'm having trouble with determining whether or not a specific word lies in a list. For example, if I have the list mylist=["This is a","test list that","serves as an example"] and say I want to determine whether or not a specific word lies in a specific index of the list:

def test():
    if "This" in mylist[0] == True:
        print("In index")
    else:
        print("Not in index")

Obviously, the word "This" lies in mylist[0], but whenever I run test(), it prints "Not in index". What is the error?

Adam Smith
  • 52,157
  • 12
  • 73
  • 112
Ayumu Kasugano
  • 440
  • 4
  • 13
  • 4
    just drop the `== True`. I'd have to go back and look up the operation order, but it's possible that instead of `if ("This" in mylist[0]) == True` it's doing `if "This" in (mylist[0] == True)`. However I would expect that to throw a `TypeError` – Adam Smith May 07 '17 at 20:42
  • My guess is that you don't need the `== True` part. `if x in y` is already valid and will show if it's in the list or not. – imrek May 07 '17 at 20:42
  • 1
    @AdamSmith that is an (valid) answer not a comment, isn't it ? – SleepProgger May 07 '17 at 20:44
  • @SleepProgger yes, but a rather trivial one and not worthy of "Answer" status without a good deal more research being done. Doubly so because I anticipate the issue with the actual code isn't being faithfully reproduced here – Adam Smith May 07 '17 at 20:45
  • @PeterWood good catch on the dupe! – Adam Smith May 07 '17 at 20:46
  • 1
    @AdamSmith I asked it myself 2.5 years ago. – Peter Wood May 07 '17 at 20:56

0 Answers0