As a data scientist working for a US-based company, I faced a scenario where I needed to verify whether a particular element existed in a Python list. In this tutorial, I will explain how to check if an array (or list) contains a specific value in Python. We will explore several methods to accomplish this.
Check if an Array Contains a Value in Python
Python provides several methods to accomplish this task, Let us see some important methods in this tutorial.
Read How to Find the Index of the Maximum Value in an Array Using Python
1. Use the ‘in’ Operator
This is the simple way to check if an element exists in a Python list is by using the 'in' operator. This operator returns True if the element is found in the list, otherwise it returns False. The ‘in’ operator is an efficient method that works well in most cases.
Example:
cities = ['New York', 'Los Angeles', 'Chicago', 'Houston']
if 'Chicago' in cities:
print("Chicago is in the list of cities.")
else:
print("Chicago is not in the list of cities.")Output:
Chicago is in the list of cities.Here is the screenshot of the executed example code.

Check out How to Update an Array in Python
2. Use the ‘not in’ Operator
In the same way, you can use the 'not in‘ operator to check if an element does not exist in a Python list.
Example:
states = ['California', 'Texas', 'Florida', 'New York']
if 'Ohio' not in states:
print("Ohio is not in the list of states.")
else:
print("Ohio is in the list of states.")Output:
Ohio is not in the list of states.Here is the screenshot of the executed example code.

Read How to Print Duplicate Elements in Array in Python
3. Use the list.count() Method
The count() method in Python provides another way to check if an element exists in a list. It returns the number of times the specified element appears in the list. If the count is greater than zero, the element exists in the list.
Example:
fruits = ['apple', 'banana', 'orange', 'apple', 'grape']
if fruits.count('apple') > 0:
print("The list contains apples.")
else:
print("The list does not contain apples.")Output:
The list contains apples.Here is the screenshot of the executed example code.

Check out How to Convert Python Dict to Array
4. Use the list.index() Method
The index() method returns the index of the first occurrence of the specified element in the list. If the element is not found, it raises a ValueError exception.
Example:
presidents = ['George Washington', 'John Adams', 'Thomas Jefferson', 'James Madison']
try:
presidents.index('Abraham Lincoln')
print("Abraham Lincoln is in the list of presidents.")
except ValueError:
print("Abraham Lincoln is not in the list of presidents.")Output:
Abraham Lincoln is not in the list of presidents.Here is the screenshot of the executed example code.

Read Python repeat array n times
5. Use the set() Function
If you have a large list and need to perform multiple checks, converting the list to a set by using set() can improve performance. The set in Python provides fast testing using hash tables.
Example:
cities = ['New York', 'Los Angeles', 'Chicago', 'Houston']
city_set = set(cities)
if 'Boston' in city_set:
print("Boston is in the list of cities.")
else:
print("Boston is not in the list of cities.")Output:
Boston is not in the list of cities.Check out How to Get Values from a JSON Array in Python
6. Use the filter() Function
Another way to check for the presence of a value based on a condition is by using the filter() function. This function constructs an iterator from elements of the list for which a function returns True.
Example:
cities = ["New York", "Los Angeles", "Chicago", "Houston", "Phoenix"]
# Check if any city contains "Angeles"
filtered_cities = list(filter(lambda city: "Angeles" in city, cities))
if filtered_cities:
print(f"Cities containing 'Angeles': {filtered_cities}")
else:
print("No cities contain 'Angeles'.")Output:
Cities containing 'Angeles': ['Los Angeles']Here, the filter() function filters the cities that contain the substring “Angeles” and returns them in a list.
Use the any() Function with a Generator Expression
In more advanced conditions, you can use the any() function in combination with a generator expression to check if any element in the list satisfies a specific condition.
Example:
numbers = [2, 4, 6, 8, 10]
if any(num > 7 for num in numbers):
print("The list contains a number greater than 7.")
else:
print("The list does not contain a number greater than 7.")Output:
The list contains a number greater than 7.Read How to Convert an Array to a Tuple in Python
Check Membership in Nested Lists
When working with nested lists (lists within lists), you can use a combination of the above methods to check for an element’s presence.
Example:
states = [['California', 'Texas'], ['Florida', 'New York'], ['Illinois', 'Ohio']]
if any('Texas' in sublist for sublist in states):
print("Texas is in the nested list of states.")
else:
print("Texas is not in the nested list of states.")Output:
Texas is in the nested list of states.Check out How to Convert Array to Set in Python
Conclusion
In this tutorial, we explored various methods to check if an array contains a value in Python. These techniques will help you efficiently. Remember to choose the suitable method based on your requirements. The 'in' and 'not in' operator is used to check if an element exists in a list or not. While the count() and index() methods provide additional functionality. Converting the list to a set by using set() when you have a large list, and use the any() function with a generator expression, using filter() method which iterator from elements of the list, we also discussed checking membership in nested lists.
Hope you found this article useful.
You may also like to read:
- How to Transpose an Array in Python
- How to Save an Array to a File in Python
- How to Check if an Array Contains a Value in Python
- How to Remove NaN from Array in Python?

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.