How to Check if an Element is Not in a List in Python?

Estimated read time 1 min read

To check if an element is not in a list in Python, you can use the not in operator.

Here’s an example:

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

if 6 not in my_list:
    print("6 is not in the list")
    print("6 is in the list")


6 is not in the list

In this example, we first define a list my_list with 5 elements. We then check if the integer 6 is not in the list using the not in operator. Since 6 is not in the list, the output of the program is "6 is not in the list".

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply