How to Find an Item in a List in Python

If you want to find an item in a Python list, there are a few simple ways to do it.

This page shows you how to:

  • Check if a list contains a value
  • Get the index of a value
  • Find items safely when they may not exist
  • Understand when to use in and when to use index()

Quick answer

fruits = ["apple", "banana", "orange"]

# Check if an item exists
if "banana" in fruits:
    print("Found it")

# Get the index of an item
position = fruits.index("banana")
print(position)  # 1

Use in when you only need to check if an item exists. Use list.index() when you need its position.

What this page helps you do

  • Check if a list contains a value
  • Get the index of a value
  • Find items safely when they may not exist
  • Understand when to use in and when to use index()

Check if an item exists with in

Use item in my_list when you want a simple True or False.

This is the best choice when you only need to know whether the value is present.

fruits = ["apple", "banana", "orange"]

print("banana" in fruits)
print("grape" in fruits)

Output:

True
False

You can also use it in an if statement:

fruits = ["apple", "banana", "orange"]

if "banana" in fruits:
    print("Banana is in the list")
else:
    print("Banana is not in the list")

This works with strings, numbers, and other values:

numbers = [10, 20, 30]

print(20 in numbers)   # True
print(50 in numbers)   # False

If you want a full beginner explanation of this pattern, see how to check if a value exists in a list in Python.

Get the position with list.index()

Use my_list.index(item) when you need the position of a value in the list.

fruits = ["apple", "banana", "orange"]

position = fruits.index("banana")
print(position)

Output:

1

Remember:

  • List indexes start at 0
  • "apple" is at index 0
  • "banana" is at index 1
  • "orange" is at index 2

index() returns the first match

If the same item appears more than once, index() only returns the first matching position.

colors = ["red", "blue", "red", "green"]

print(colors.index("red"))

Output:

0

If you want more detail on this method, see the Python list index() method.

What happens if the item is missing

If the item is not in the list, index() raises a ValueError.

fruits = ["apple", "banana", "orange"]

print(fruits.index("grape"))

This causes an error because "grape" is not in the list.

How to avoid errors when using index()

When an item may be missing, do not call index() blindly.

Option 1: Check with in first

This is often the easiest approach for beginners.

fruits = ["apple", "banana", "orange"]

if "banana" in fruits:
    position = fruits.index("banana")
    print(position)
else:
    print("Item not found")

Option 2: Use try and except ValueError

This is useful when you want to handle the error directly.

fruits = ["apple", "banana", "orange"]

try:
    position = fruits.index("grape")
    print(position)
except ValueError:
    print("Item not found")

Both approaches are valid.

  • Use in first if you want simple, readable code
  • Use try and except if the missing value is part of normal program flow

If you run into this kind of problem often, learning safe looping and checking patterns will help. A good next step is how to loop through a list in Python.

Find all matching positions

If a value appears more than once, index() is not enough because it only gives you the first match.

To find every matching index, loop through the list with enumerate().

colors = ["red", "blue", "red", "green", "red"]

matches = []

for i, value in enumerate(colors):
    if value == "red":
        matches.append(i)

print(matches)

Output:

[0, 2, 4]

Why enumerate() helps

enumerate() gives you both:

  • the index
  • the value

That makes it very useful when searching through a list.

colors = ["red", "blue", "red"]

for i, value in enumerate(colors):
    print(i, value)

Output:

0 red
1 blue
2 red

If enumerate() is new to you, see Python enumerate() explained.

Choosing the right approach

Pick the method based on what you need:

  • Use in for existence checks
  • Use index() for the first position
  • Use enumerate() in a loop for all positions
  • Pick the simplest method for your task

A quick summary:

items = ["a", "b", "a"]

# Exists?
print("b" in items)          # True

# First position
print(items.index("a"))      # 0

# All positions
positions = []
for i, value in enumerate(items):
    if value == "a":
        positions.append(i)

print(positions)             # [0, 2]

Common mistakes

Here are some common reasons list searching does not work as expected:

  • Using index() without checking whether the item exists
  • Expecting index() to return all matches instead of the first one
  • Forgetting that list indexes start at 0
  • Checking for a string with different capitalization, such as Banana vs banana
  • Comparing different data types, such as '5' and 5

These quick checks can help you debug:

print(my_list)
print(item in my_list)
print(type(item))
print([type(x) for x in my_list])
for i, value in enumerate(my_list):
    print(i, value)

These are useful for spotting:

  • unexpected values in the list
  • type problems
  • capitalization differences
  • duplicate items

FAQ

How do I check if an item is in a list in Python?

Use the in operator:

if item in my_list:
    print("Found")

How do I get the index of an item in a list?

Use my_list.index(item). This returns the first matching index.

letters = ["a", "b", "c"]
print(letters.index("b"))

What happens if the item is not in the list?

If you use index(), Python raises ValueError.

Use in first or catch the error with try and except.

letters = ["a", "b", "c"]

if "z" in letters:
    print(letters.index("z"))
else:
    print("Not found")

How do I find all positions of a value in a list?

Loop through the list with enumerate() and collect every matching index.

numbers = [1, 2, 1, 3, 1]

positions = []

for i, value in enumerate(numbers):
    if value == 1:
        positions.append(i)

print(positions)

Can I search for part of a string inside a list?

Not directly with in for whole list items.

For example, this checks for a full item:

words = ["apple", "banana", "orange"]
print("app" in words)   # False

If you want partial matches inside each string, use a loop:

words = ["apple", "banana", "orange"]

for word in words:
    if "app" in word:
        print(word)

See also