• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Python not in – Check if Value is Not Included in Object

Python not in – Check if Value is Not Included in Object

August 30, 2022 Leave a Comment

You can use the not and in operators in Python to check if a value is not included in objects such as lists, dictionaries, sets, tuples, etc.

x = "something"

l = ["this","is","a","list"]
d = {"a":1,"b":2,"c":3}
t = (0, 1, 2)
s = {0, 1, 2}
st = "string"

print(x not in l)
print(x not in d)
print(x not in t)
print(x not in s)
print(x not in st)

#Output:
True
True
True
True
True

When working with data, the ability to easily check if a certain value is included in your data is valuable.

In Python, there are many useful operators which give us the ability to perform many operations and make our lives easier.

Two of the most commonly used operators in Python are the in and not operators.

in allows you to check if a value is in an object. not negates the value of a boolean.

You can combine in and not to check if a value is not in an object in Python.

The rest of this article has some examples of how to check if a value is not in an object in Python.

How to Check if Value is Not in List Using Python

One of the most common examples of using not and in in Python is to check if a value is not in a list.

In Python, lists are a collection of objects which are unordered. When working with lists, it can be useful to be able to easily check if a value is in a list or not.

Below are some examples of how you can check if a value is not in a list using Python.

x = "a"

lst1 = ["a","b","c","d"]
lst2 = ["this","is","a","list"]
lst3 = [0, 1, 2, 3]

print(x not in lst1)
print(x not in lst2)
print(x not in lst3)

#Output:
False
True
True

How to Check if Key is Not in Dictionary Using Python

Another examples of using not and in in Python is to check if a key is not in a dictionary.

In Python, dictionaries are a collection of key/value pairs separated by commas. When working with dictionaries, it can be useful to be able to easily check if a key is in a dictionary, or a value is in a dictionary.

You can check if a key is not in a dictionary with in and not.

To check if a value is not in a dictionary, you can use the values() function to get the values and then use not and in

Below are some examples of how you can check if keys and values are not in a dictionary using Python.

a = "a"
z = "z"
b = 2
d = 4

d = {"a":1,"b":2,"c":3}

print(a not in d)
print(z not in d)
print(b not in d.values())
print(d not in d.values())

#Output:
False
True
False
True

How to Check if Value is Not in Tuple Using Python

Another example of using not and in in Python is checking if a value is not in a tuple.

In Python, tuples are a collection of objects which are ordered and mutable. When working with tuples, it can be useful to be able to easily determine if the tuple is empty.

Below are some examples of how you can check if a value is not in a tuple using Python.

x = "x"
y = 0

t = (0, 1, 2)

print(x not in t)
print(y not in t)

#Output:
True
False

How to Check if Value is Not in Set Using Python

One more example of using not and in in Python is checking if a value is not in a set.

In Python, sets are a collection of elements which is unordered and mutable. When working with sets, it can be useful to be able to easily determine if the set is empty.

Below are some examples of how you can check if a value is not in a set using Python.

x = "x"
y = 0

t = {0, 1, 2}

print(x not in t)
print(y not in t)

#Output:
True
False

How to Check if Character or Substring is Not in String Using Python

One final example of using not and in in Python is checking if a character or substring is not in a string.

Below are some examples of how you can check if a character or substring is not in a string using Python.

c = "c"
k = "str"

st = "string"

print(c not in str)
print(k not in str)

#Output:
True
False

Hopefully this article has been useful for you to learn how to check if a value is not in an object using not and in in Python.

Other Articles You'll Also Like:

  • 1.  Break Out of Function in Python with return Statement
  • 2.  Check if Number is Larger than N in Python
  • 3.  Convert pandas Series to List in Python
  • 4.  Get First Key and Value in Dictionary with Python
  • 5.  Divide Each Element in List by Scalar Value with Python
  • 6.  Using Python to Remove Non-Empty Directory
  • 7.  Calculating Sum of Squares in Python
  • 8.  Get Length of File Using Python
  • 9.  How to Check If Value is in List Using Python
  • 10.  pandas max – Find Maximum Value of Series or DataFrame

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy