How to Check If Something Is Not In A Python List

Avatar

By squashlabs, Last Updated: Nov. 2, 2023

How to Check If Something Is Not In A Python List

To check if something is not in a Python list, you can use the not in operator. This operator allows you to check if a value is not present in a list. It returns True if the value is not found in the list, and False otherwise.

Here are two ways to check if something is not in a Python list:

1. Using the not in operator

You can use the not in operator to check if a value is not present in a list. The syntax is as follows:

value not in list

Here, value is the value you want to check for, and list is the list in which you want to search for the value.

Example:

numbers = [1, 2, 3, 4, 5]

# Check if 6 is not in the list
if 6 not in numbers:
    print("6 is not in the list")

# Check if 3 is not in the list
if 3 not in numbers:
    print("3 is not in the list")

Output:

6 is not in the list

In the example above, the first if statement is true because 6 is not present in the numbers list. The second if statement is false because 3 is present in the numbers list.

Related Article: String Interpolation in Python Explained

2. Using the in operator with the not keyword

Alternatively, you can use the in operator with the not keyword to achieve the same result. The syntax is as follows:

not value in list

Example:

numbers = [1, 2, 3, 4, 5]

# Check if 6 is not in the list
if not 6 in numbers:
    print("6 is not in the list")

# Check if 3 is not in the list
if not 3 in numbers:
    print("3 is not in the list")

Output:

6 is not in the list

In this example, the output is the same as the previous example. The first if statement is true because 6 is not present in the numbers list, and the second if statement is false because 3 is present in the numbers list.

These are the two ways to check if something is not in a Python list. You can choose the one that you find more readable or intuitive.

Best Practices

Related Article: How to Define a Function with Optional Arguments in Python

When checking if something is not in a Python list, keep the following best practices in mind:

- Use the not in operator or the in operator with the not keyword, as they provide a concise and readable way to perform the check.

- If you need to check for multiple values, you can use a loop or list comprehension to iterate over the values and perform the check for each value.

- Consider using sets instead of lists if you have a large number of values to check, as sets offer faster membership tests compared to lists.

These best practices can help you write clean and efficient code when checking if something is not in a Python list.

More Articles from the Python Tutorial: From Basics to Advanced Concepts series:

How to Use Redis with Django Applications

Using Django Redis in Python programming can greatly enhance the performance and scalability of your Django applications. This guide covers everythin… read more

How to do Incrementing in Python

Learn how to use incrementing in Python coding with this comprehensive guide. From understanding the Python increment operator to working with increm… read more

How to Check If a Variable Exists in Python

Verifying the existence of a variable in Python code is a fundamental skill for any programmer. This article provides a simple guide on how to check … read more

How to Use Switch Statements in Python

Switch case statements are a powerful tool in Python for handling multiple conditions and simplifying your code. This article will guide you through … read more

How to Integrate Python with MySQL for Database Queries

Pair Python with MySQL to execute database queries effortlessly. Learn about the Python MySQL Connector, establishing connections, interacting with M… read more

How to Install Specific Package Versions With Pip in Python

Guide on installing a specific version of a Python package using pip. Learn different methods such as using the == operator, specifying version range… read more

Integrating Django Apps with Chat, Voice & Text

Integrate SMS gateways, build voice apps, and more with Django. Learn about Django chat applications, WebRTC integration, and SMS gateways in Django.… read more

Advance Django Forms: Dynamic Generation, Processing, and Custom Widgets

Dynamic form generation, formsets, inline formsets, custom widgets, and validation in Django are all important aspects of building robust and web app… read more

How To Handle Ambiguous Truth Values In Python Arrays

Handling ambiguous truth values in Python arrays can be a challenge, but with the "a.any()" and "a.all()" methods, you can easily manage these errors… read more

Python Type: How to Use and Manipulate Data Types

Learn how to use and manipulate data types in Python with this tutorial. Explore the fundamentals of numeric, textual, sequence, mapping, set, boolea… read more