Skip to content
Fix Code Error

Check if something is (not) in a list in Python

March 13, 2021 by Code Error
Posted By: Anonymous

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?

Solution

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
Answered By: Anonymous

Related Articles

  • Why call git branch --unset-upstream to fixup?
  • Difference between git checkout --track…
  • What are "named tuples" in Python?
  • Why do I have to "git push --set-upstream origin "?
  • AttributeError: 'str' object has no attribute 'append'
  • Why does git perform fast-forward merges by default?
  • How to append multiple items in one line in Python
  • Using global variables between files?
  • What is a tracking branch?
  • How to create the branch from specific commit in…
  • How do the PHP equality (== double equals) and…
  • Kotlin's List missing "add", "remove", Map missing…
  • Can't install via pip because of egg_info error
  • Delete all objects in a list
  • What's the difference between lists and tuples?
  • ArrayList insertion and retrieval order
  • Checkout another branch when there are uncommitted…
  • Git Using Remote Branch
  • Why do git fetch origin and git fetch : behave differently?
  • VueJS: How to use a binding value within a for loop…
  • What's the difference between eval, exec, and compile?
  • TypeError: 'tuple' object is not callable when…
  • Why is processing a sorted array faster than…
  • Your configuration specifies to merge with the from…
  • How do I delete a Git branch locally and remotely?
  • How to print a date in a regular format?
  • jQuery load first 3 elements, click "load more" to…
  • How to recover stashed uncommitted changes
  • Remove similar tuple from dictionary of tuples
  • Check if list contains any dict keys
  • What are the undocumented features and limitations…
  • Should a global object be accessed by a function…
  • How can I reconcile detached HEAD with master/origin?
  • What exactly does the "u" do? "git push -u origin…
  • Python 3.9+ typing.Collection vs tuple vs typing.Iterable
  • Checking for empty or null List
  • C++ pointer vs object
  • Why did my Git repo enter a detached HEAD state?
  • How to compare each item in a list with the rest, only once?
  • Git workflow and rebase vs merge questions
  • In plain English, what does "git reset" do?
  • How do SO_REUSEADDR and SO_REUSEPORT differ?
  • Check if list contains element that contains a…
  • Various ways to remove local Git changes
  • How do you stop tracking a remote branch in Git?
  • python 3.2 UnicodeEncodeError: 'charmap' codec can't…
  • Remove tracking branches no longer on remote
  • Usage of __slots__?
  • What does the "yield" keyword do?
  • Make: "nothing to be done for target" when invoking…
  • javascript .replace and .trim not working in vuejs
  • How do I clone a single branch in Git?
  • Assigning values to tuple in for loop
  • Why does C++ code for testing the Collatz conjecture…
  • Smart way to truncate long strings
  • Git merge master into feature branch
  • Polymer Dart @initMethod not executing
  • Simple Syntax Question (tuple conversion)
  • Ukkonen's suffix tree algorithm in plain English
  • Delete all local git branches
  • What does the term "Tuple" Mean in Relational Databases?
  • How does tuple comparison work in Python?
  • Append a tuple to a list - what's the difference…
  • Pythonic way to print list items
  • Python, TypeError: unhashable type: 'list'
  • Why do I need to do `--set-upstream` all the time?
  • Why does Git say my master branch is "already up to…
  • How to download a branch with git?
  • how to Display Tuple{Float64, Bool}
  • How to cherry pick a range of commits and merge into…
  • C# HTTPClient Not Sending Request
  • I tried to define a dynamically growing array using…
  • Git: Create a branch from unstaged/uncommitted…
  • What's the difference between lists enclosed by…
  • How to group by sequence of tags?
  • Does Java SE 8 have Pairs or Tuples?
  • Is there a Haskell lens function for "zipping"…
  • How do I test a computed property on a component in Ember?
  • BootstrapVue: B-table item data not running…
  • problem with client server unix domain stream…
  • Does Python have a ternary conditional operator?
  • moving committed (but not pushed) changes to a new…
  • How to update Python?
  • Do you (really) write exception safe code?
  • Git fetch remote branch
  • Update a local branch with the changes from a…
  • Julia, efficient way to create a DataFrame by…
  • How to clone ('fork') your own personal GitHub repo…
  • How do you get git to always pull from a specific branch?
  • Git pull a certain branch from GitHub
  • How to test multiple variables against a value?
  • How can I wrap all BeautifulSoup existing…
  • How does origin/HEAD get set?
  • Merge, update, and pull Git branches without using checkouts
  • Some of Icon click listeners work and some not in a…
  • Remove duplicate dict in list in Python
  • How do I merge two dictionaries in a single…
  • Convert json data to a html table
  • Throw HttpResponseException or return…
  • How do I implement IEnumerable

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

application/x-www-form-urlencoded or multipart/form-data?

Next Post:

How do I check for null values in JavaScript?

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

.net ajax android angular arrays aurelia backbone.js bash c++ css dataframe ember-data ember.js excel git html ios java javascript jquery json laravel linux list mysql next.js node.js pandas php polymer polymer-1.0 python python-3.x r reactjs regex sql sql-server string svelte typescript vue-component vue.js vuejs2 vuetify.js

  • you shouldn’t need to use z-index
  • No column in target database, but getting “The schema update is terminating because data loss might occur”
  • Angular – expected call-signature: ‘changePassword’ to have a typedeftslint(typedef)
  • trying to implement NativeAdFactory imports deprecated method by default in flutter java project
  • What should I use to get an attribute out of my foreign table in Laravel?
© 2022 Fix Code Error