Python lists are quite versatile and can store a wide range of data in a sequential manner. It might be handy to know if an element belongs to a list or not. For example, you are a librarian at a public library, a person wants to check whether a book is available or not. Fortunately, you maintain a record of books available in the library as a python list and can quickly check this. In this tutorial, we will look at how to check whether an element is present in a python list or not.
How to check if an element is present in a list?
The best way to check if an element is in a python list is to use the membership operator in
. The following is the syntax:
# here ls is a list of values a in ls
The above expression returns a boolean value, True
if a
is present in the list ls
and False
if its not.
There are other methods as well to check for an element’s presence in a list but the above is probably the most simple and a considerably fast method. Let’s look at some examples to demonstrate its the usage.
# list of books available_books = ["1984", "Animal Farm", "Dracula", "Moby Dick", "The Great Gatsby"] # check if element in list "Annabel" in available_books
Output:
False
We get False as the output since the element “Annabel” was not present in the list “available_books”. Let’s now check for a book that is present in the list.
# list of books available_books = ["1984", "Animal Farm", "Dracula", "Moby Dick", "The Great Gatsby"] # check if element in list "Animal Farm" in available_books
Output:
True
You can see we get True as the output since the element “Animal Farm” is present in the list “available_books”.
Since lists are ordered, you can also get the index of the element you’re looking for using the list index()
function. For example, let’s find the index of the above element in the list “available_books”.
# get the index of element if "Animal Farm" in available_books: print(available_books.index("Animal Farm"))
Output:
1
We get 1 as the output. That is, the book “Animal Farm” is present at the 1st index (starting from 0).
If you’re not interested in the index and want to further speed up your code, you can first convert the list to a set and then check for the element in it using the in
operator.
# check if element in set "Animal Farm" in set(available_books)
Output:
True
We get True as the output since the element is present in the set.
For more on operators in python, refer to our comprehensive tutorial covering a wide range of operators used in python.
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.
Tutorials on python lists:
- Python – Check if an element is in a list
- Python – Iterate over multiple lists in parallel using zip()
- Python – Flatten a list of lists to a single list
- Pandas DataFrame to a List in Python
- Python – Convert List to a String
- Convert Numpy array to a List – With Examples
- Python List Comprehension – With Examples
- Python List Index – With Examples
- Python List Count Item Frequency
- Python List Length
- Python Sort a list – With Examples
- Python Reverse a List – With Examples
- Python Remove Duplicates from a List
- Python list append, extend and insert functions.
- Python list remove, pop and clear functions.