I have a list of tuples in Python, and I have a conditional where I want to take the branch ONLY if the tuple is not in the list (if it is in the list, then I don't want to take the if branch)
if curr_x -1 > 0 and (curr_x-1 , curr_y) not in myList: # Do Something
This is not really working for me though. What have I done wrong?
“not in” operator − This operator is used to check whether an element is not present in the passed list or not. Returns true if the element is not present in the list otherwise returns false.
Use the any() function to check if an object exists in a list of objects. The any() function will return True if the object exists in the list, otherwise False is returned.
Besides the Find and Replace function, you can use a formula to check if a value is in a list. Select a blank cell, here is C2, and type this formula =IF(ISNUMBER(MATCH(B2,A:A,0)),1,0) into it, and press Enter key to get the result, and if it displays 1, indicates the value is in the list, and if 0, that is not exist.
The simplest and fastest way to check whether a string contains a substring or not in Python is the "in" operator . This operator returns true if the string contains the characters, otherwise, it returns false .
The bug is probably somewhere else in your code, because it should work fine:
>>> 3 not in [2, 3, 4] False >>> 3 not in [4, 5, 6] True
Or with tuples:
>>> (2, 3) not in [(2, 3), (5, 6), (9, 1)] False >>> (2, 3) not in [(2, 7), (7, 3), "hi"] True
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With