How to Check If Something Is Not In A Python List

Avatar

By squashlabs, Last Updated: November 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: How To Convert a Dictionary To JSON In Python

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

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.

Related Article: How to Sort a Dictionary by Key in Python

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

How to Remove a Key from a Python Dictionary

Removing a key from a Python dictionary is a common task in programming. This guide provides step-by-step instructions on how to achieve this using the del statement,... read more

How to Remove an Element from a List by Index in Python

A guide on removing elements from a Python list by their index. Methods include using the 'del' keyword, the 'pop()' method, the 'remove()' method, list comprehension,... read more

How to Solve a Key Error in Python

This article provides a technical guide to resolving the KeyError issue in Python. It covers various methods such as checking if the key exists before accessing it,... read more

How to Add New Keys to a Python Dictionary

Adding new keys and their corresponding values to an existing Python dictionary can be achieved using different methods. This article provides a guide on two popular... read more

How to Read a File Line by Line into a List in Python

Reading a file line by line into a list in Python is a common task for many developers. In this article, we provide a step-by-step guide on how to accomplish this using... read more

How to Find a Value in a Python List

Are you struggling to find a specific value within a Python list? This guide will show you how to locate that value efficiently using different methods. Whether you... read more