Check if an element is not in a list in Python
In this tutorial, we will show you how to check if an element is not in a list in Python.
In Python, not in
use
The keyword in Python in
can be used to check if an element exists in a set. If an element exists, then it returns True
; otherwise, it returns False
. For example:
x = 3 in [1, 2, 5]
y = 1 in [1, 2, 5]
print(x)
print(y)
Output:
False
True
If we need to check if an element is not in the list, we can use not in
the - keyword. - not
is a logical operator that True
converts - to - False
and vice versa. So if an element is not in the list, it will be returned True
.
x = 3 not in [1, 2, 5]
print(x)
Output:
True
Use the list __contains__
method to check if an element is not in the list
In Python, we have some magic functions associated with classes that are not meant to be called directly, although possible. One of these __contains__
functions called can be used to check if an element exists in a list. For example:
x = [1, 2, 5].__contains__(1)
print(x)
x = [1, 2, 5].__contains__(3)
print(x)
Output:
True
False
While this approach works, it is not recommended because magic functions are not set up to be called directly.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Finding a string in a list in Python
Publish Date:2025/05/09 Views:75 Category:Python
-
This tutorial shows you how to find elements from a Python list that have a specific substring in them. We will use the following list and extract ack the strings that have in it. my_list = [ "Jack" , "Mack" , "Jay" , "Mark" ] for Find elem
Getting list shape in Python
Publish Date:2025/05/09 Views:139 Category:Python
-
In Python, knowing the shape of a list is very important for working with data structures, especially when it comes to multidimensional or nested lists. This article explores various ways to determine the shape of a list in Python, from sim
Adding multiple elements to a list in Python
Publish Date:2025/05/09 Views:180 Category:Python
-
List is a mutable data structure in Python. It can contain values of different types. This article will discuss some methods to append single or multiple elements to a Python list. append() Append a single element in a Python list usi
Get the index of the maximum and minimum values in a list in Python
Publish Date:2025/05/09 Views:117 Category:Python
-
In this tutorial, we will discuss methods to get the index of the maximum and minimum value of a list in Python. max() Get the index of the maximum value in a list using and list.index() functions in Python max() The function gives the maxi
List of numbers from 1 to N in Python
Publish Date:2025/05/09 Views:116 Category:Python
-
This tutorial will discuss how to create a list of numbers from 1 to some specified number. Create a user-defined function to create a list of numbers from 1 to N This method will take the desired number from the user and for iterate until
Convert List to Pandas DataFrame in Python
Publish Date:2025/05/09 Views:133 Category:Python
-
This article will show you how to convert items in a list into a Pandas DataFrame. Convert List to Pandas DataFrame in Python DataFrame, in general, is a two-dimensional labeled data structure. Pandas is an open source Python package that i
Sorting a list by another list in Python
Publish Date:2025/05/09 Views:148 Category:Python
-
Normally, when we sort a list, we do it in ascending or descending order. However, we can sort a list based on the order of another list in Python. We will learn how to sort a given list based on the values in another list in this art
Normalizing a list of numbers in Python
Publish Date:2025/05/09 Views:134 Category:Python
-
Normalization means converting the given data to another scale. We rescale the data so that it is between two values. Most of the time, the data is rescaled between 0 and 1. We rescale data for different purposes. For example, machine learn
How to create a list of a specific size in Python
Publish Date:2025/05/09 Views:195 Category:Python
-
Preallocating storage for a list or array is a common practice among programmers when they know the number of elements in advance. Unlike C++ and Java, in Python you must initialize all preallocated storage with some value. Typically, devel