How to Check if a Value Exists in a List in Python
If you want to check whether a value is inside a Python list, the simplest solution is the in operator.
This is the most common beginner-friendly way to test list membership. It works for strings, numbers, and other values stored in a list.
Quick answer
items = ["apple", "banana", "orange"]
if "banana" in items:
print("Found")
else:
print("Not found")
Output:
Found
Use in for a simple membership check. It returns True if the value is present, otherwise False.
Use the in operator
The main way to check whether a value exists in a list is with in.
It gives you a Boolean result:
Trueif the value exists in the listFalseif it does not
Example:
numbers = [10, 20, 30, 40]
print(20 in numbers)
print(99 in numbers)
Output:
True
False
You can also use it inside an if statement:
colors = ["red", "green", "blue"]
if "green" in colors:
print("The list contains green")
This is usually the best starting point for beginners. If you are still learning how lists work, see the guide to Python lists for beginners.
Use not in to check that a value is missing
Use not in when you want to confirm that a value is not present.
Example:
items = ["pen", "pencil", "eraser"]
if "marker" not in items:
print("marker is not in the list")
Output:
marker is not in the list
This often makes conditions easier to read.
It is also useful before adding a new item:
tags = ["python", "beginner", "lists"]
if "loops" not in tags:
tags.append("loops")
print(tags)
Output:
['python', 'beginner', 'lists', 'loops']
If you want to do more than just check membership, such as locating an item, read how to find an item in a list in Python.
Understand exact matching
The in operator checks for an exact item match in the list.
That means:
"apple"matches"apple""Apple"does not match"apple"1does not match"1"
Case-sensitive string matching
String membership checks are case-sensitive.
fruits = ["apple", "banana", "orange"]
print("apple" in fruits)
print("Apple" in fruits)
Output:
True
False
Full item match, not substring match
If a list contains full strings, in checks whether one full string is an item in the list.
words = ["apple pie", "banana bread", "orange juice"]
print("apple" in words)
print("apple pie" in words)
Output:
False
True
Even though "apple" is part of "apple pie", it is not a full list item.
Matching data types
Different types are treated as different values.
values = ["1", "2", "3"]
print(1 in values)
print("1" in values)
Output:
False
True
If you are working with user input or mixed data, this is a very common source of bugs.
Check lists of strings safely
Sometimes case should not matter. For example, you may want "ADMIN" and "admin" to count as the same value.
A common solution is to convert both the value and the list items to lowercase.
names = ["Alice", "Bob", "Charlie"]
search_name = "alice"
if search_name.lower() in [name.lower() for name in names]:
print("Found")
else:
print("Not found")
Output:
Found
This is useful for:
- names
- commands
- tags
- user input
You can also store the lowercase version in a variable to make the code clearer:
commands = ["Start", "Stop", "Pause"]
user_input = "start"
normalized_commands = [command.lower() for command in commands]
if user_input.lower() in normalized_commands:
print("Valid command")
Output:
Valid command
When to use a loop instead
Use in when you only need a simple exact match.
Use a loop when you need something more flexible, such as:
- partial matches
- ignoring extra spaces
- checking only part of each item
- custom comparison rules
For example, to find whether any list item contains "apple" as part of a larger string:
items = ["apple pie", "banana bread", "orange juice"]
found = False
for item in items:
if "apple" in item:
found = True
break
print(found)
Output:
True
Here, the loop checks inside each string item.
You might also use a loop when cleaning text before comparing:
items = [" Apple ", "Banana", "Orange"]
search = "apple"
found = False
for item in items:
if item.strip().lower() == search.lower():
found = True
break
print(found)
Output:
True
Do not use a loop for a simple exact check if in already solves the problem. The in operator is shorter and easier to read.
If you need to keep only matching items, see how to filter a list in Python.
Common mistakes
Here are some common reasons a list membership check does not work as expected.
Using == instead of in
This compares one value to the whole list, which is usually not what you want.
Wrong:
items = ["apple", "banana", "orange"]
print("banana" == items)
Correct:
print("banana" in items)
Expecting case-insensitive matching without converting text
This will fail because the strings do not match exactly:
items = ["apple", "banana", "orange"]
print("Apple" in items)
If case should not matter, convert both sides to lowercase.
Checking for a substring when the list contains full strings
This can be confusing:
items = ["apple pie", "banana bread"]
print("apple" in items)
This returns False because "apple" is not a full item in the list.
If you need substring matching, use a loop.
Comparing different data types
Be careful with numbers and strings:
items = ["1", "2", "3"]
print(1 in items)
print("1" in items)
These produce different results because 1 and "1" are different types.
Forgetting that nested lists need a different check
If your list contains lists, you are checking for a full nested item.
data = [[1, 2], [3, 4]]
print([1, 2] in data)
print(1 in data)
Output:
True
False
1 is inside a nested list, but it is not a top-level item in data.
Simple debugging checks
If your result is not what you expect, print the values you are comparing:
print(my_list)
print(value)
print(type(value))
print(value in my_list)
print([type(item) for item in my_list])
print([item.lower() for item in my_list])
These quick checks help you spot:
- wrong values
- wrong types
- unexpected capitalization
If you run into string search problems elsewhere, you may also want to read how to fix ValueError: substring not found.
FAQ
How do I check if a value is in a list in Python?
Use the in operator:
value in my_list
How do I check if a value is not in a list?
Use not in:
value not in my_list
Is the check case-sensitive?
Yes. "Apple" and "apple" are different strings.
Can I check part of a string inside a list item with in?
Not directly for list membership. in checks whether a full item exists in the list.
If you want to check whether part of a string appears inside any list item, use a loop.
What if my list contains numbers as strings?
Make sure the types match. 5 is different from "5".