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.

Categorized in:

Python,

Last Update: February 26, 2024