As a Python developer working on various projects, I came across a scenario where I needed to check whether a specific element was present or not in the list. After researching various methods I found a few important ways to achieve this task. In this article, I will explain how to check if an element is not in a list in Python with examples.
Check if an Element is Not in a List in Python
To check if an element is not in a list in Python, you can use the operator not in . For example, if you have a list called fruits with elements ['apple', 'banana', 'orange'] you can check if 'grape' is not in the list by using the code: if 'grape' not in fruits:. This will return True since 'grape' is not present in the fruits list.
Read How to Add Elements to an Empty List in Python?
1. Use the not in Operator
The easiest way to check if an element is not in a list is by using the not in operator in Python. This operator returns True if the specified element is not found in the list, and False otherwise. Here’s an example:
states = ['California', 'New York', 'Texas', 'Florida']
if 'Ohio' not in states:
print("Ohio is not in the list of states.")
else:
print("Ohio is in the list of states.")Output:
Ohio is not in the list of states.You can see the output in the below screenshot.

In this example, we have a list called states containing some popular states in the USA. We use the not in operator to check if the string 'Ohio' is not present in the states list. Since 'Ohio' is indeed not in the list, the code inside the if block will be executed, printing the message “Ohio is not in the list of states.
Check out How to Remove None Values from a List in Python?
2. Use if Statement with not in operator
Another way to check if an element is not in a Python list is by using the if not in conditional statement. This approach combines the if statement with the not in operator to create a more concise and readable code. Here’s an example:
presidents = ['George Washington', 'John Adams', 'Thomas Jefferson', 'James Madison']
name = 'Abraham Lincoln'
if name not in presidents:
print(f"{name} was not one of the first four presidents of the United States.")Output:
Abraham Lincoln was not one of the first four presidents of the United States.You can see the output in the below screenshot.

In this example, we have a list called presidents that contains the names of the first four presidents of the United States. We want to check if the name 'Abraham Lincoln' is not in the presidents list. By using the if not in statement, we can directly test the condition. If 'Abraham Lincoln' is not found in the list, the code inside the if block will be executed, printing the appropriate message.
Check Multiple Elements
Sometimes, you may need to check if multiple elements are not present in a Python list. You can achieve this by using the not in operator with the and or or logical operators. Here’s an example:
cities = ['New York City', 'Los Angeles', 'Chicago', 'Houston']
if 'Boston' not in cities and 'Philadelphia' not in cities:
print("Both Boston and Philadelphia are not in the list of cities.")Output:
Both Boston and Philadelphia are not in the list of cities.You can see the output in the below screenshot.

In this example, we have a list called cities containing some major cities in the USA. We want to check if both 'Boston' and 'Philadelphia' are not on the cities list. By using the not in operator with the and logical operator, we can test both conditions simultaneously. If both cities are not found in the list, the code inside the if block will be executed, printing the corresponding message.
Read How to Find the Largest Number in a List Using Python?
3. Use all() Function
When you need to check if multiple elements are not in a Python list, you can also use the all() function in combination with a generator expression. The all() function returns True if all elements in an iterable are true. Here’s an example:
tech_companies = ['Apple', 'Google', 'Microsoft', 'Amazon']
if all(company not in tech_companies for company in ['Facebook', 'Netflix']):
print("Neither Facebook nor Netflix are in the list of tech companies.")In this example, we have a list called tech_companies that includes some well-known technology companies in the USA. We want to check if both 'Facebook' and 'Netflix' are not present in the tech_companies list. By using the all() function with a generator expression, we can test if all elements in the specified list are not in the tech_companies list. If both conditions are satisfied, the code inside the if block will be executed, printing the desired message.
Check out How to Divide Each Element in a List by a Number in Python?
Handle User Input
In real-world scenarios, you often need to handle user input and validate if it exists in a predefined list. Let’s consider an example where we prompt the user to enter their favorite sport and check if it’s not in a list of popular sports in the USA.
popular_sports = ['Football', 'Basketball', 'Baseball', 'Soccer']
favorite_sport = input("Enter your favorite sport: ")
if favorite_sport not in popular_sports:
print(f"{favorite_sport} is not one of the most popular sports in the USA.")
else:
print(f"Great choice! {favorite_sport} is a popular sport in the USA.")In this example, we have a list called popular_sports that contains some of the most popular sports in the United States. We prompt the user to enter their favorite sport using the input() function and store it in the favorite_sport variable. Then, we use the not in operator to check if the user’s input is not in the popular_sports list. If it’s not found, we print a message indicating that their favorite sport is not one of the most popular ones in the USA. Otherwise, we print a message acknowledging their choice as a popular sport.
Read How to Find the Closest Value in a List Using Python?
Optimize Performance
When working with large lists, checking if an element is not in the list using the not in operator can be inefficient, especially if you need to perform this check repeatedly. In such cases, you can optimize performance by converting the list to a set before performing the membership test. Sets in Python are optimized for fast membership testing. Here’s an example:
countries = ['USA', 'Canada', 'Mexico', 'Brazil', 'Argentina', 'Colombia', 'Peru']
# Convert the list to a set
country_set = set(countries)
if 'Chile' not in country_set:
print("Chile is not in the list of countries.")In this example, we have a list called countries that includes some countries in the Americas. To optimize the membership test, we convert the countries list to a set using the set() function and store it in the country_set variable. Then, we use the not in operator to check if 'Chile' is not in the country_set. By using a set instead of a list, the membership test becomes much faster, especially for larger datasets.
Check out How to Sort a List in Python Without Using the sort() Function?
Conclusion
In this tutorial, I helped you to understand how to check if an element is not in a list in Python. I discussed using the not in operator, using the if Statement with not in operator, checking multiple elements, and using all() function. I also covered handling user input and optimizing performance.
You can read:
- How to Find the Index of the Maximum Value in a List using Python?
- How to Capitalize the First Letter of Every Word in a List using Python?
- How to Check if Any Element in a List is Present in Another List using Python?

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.