Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (47.6k points)

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?

1 Answer

0 votes
by (106k points)

To check if something is (not) in a list in python you can do by the following way, but the error in your code is somewhere else:

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

Related questions

0 votes
1 answer
0 votes
4 answers
0 votes
2 answers
asked May 30, 2019 in Python by Anvi (10.2k points)
0 votes
4 answers
0 votes
1 answer

Browse Categories

...