Find Match in List in Python (3 Examples)


Hi! This tutorial will show you 3 ways to detect a match in a list in the Python programming language.

Here is an overview:

Let’s get into the Python code!


Create Example Python List of Integer Values

We will here create the example Python list of integer values that we will use in the examples in this tutorial. Therefore, in your Python coding IDE, run the code below to create the example list of integers:

my_list = [2,4,6,8,10,12]
# [2, 4, 6, 8, 10, 12]
# <class 'list'>

With the example Python list of integers created, we will now examine 3 ways to determine a match in the list. Below is the target value whose match in the list we will determine:

target = 8

Let’s find the target value!


Example 1: Determine Match in List Using in Operator & Conditional if Statement

In this first example, we are going to use the in operator and the conditional if statement to detect the match in the list:

if target in my_list: print(True)
else: print(False)
# True

In the above example, by using the in operator, we easily determine whether a specific value is present in the given list. The if statement checks if the value 8 is present in my_list.

If it is found, the code inside the if block executes, printing the boolean value True. However, if the value 8 was not present in the list, the code inside the else block would execute and print False.


Example 2: Determine Match in List Using index() Method

In this next example, we will use the built-in index() method to get the match in the list, like so:

except ValueError:
# True

The index() method in the above example is used to find the index or position of the first occurrence of the target within a list. It searches through the list to find the first occurrence of the target.

If a match is found, the method returns the index of that match, which represents its position within the list. However, if the value is not present in the list, the index() method raises a ValueError exception.

To handle this situation, we use a try-except block to catch the exception and print True, if the index of the target is found, and False, if it is not found.

Example 3: Determine Match in List Using for Loop

In this last example, we will use a for loop to determine the match in the list:

for search in my_list:
  if search == target:
# True

Here, the loop iterates over each element in the list, and in each iteration, it assigns the current element to the variable search.

Inside the loop, an if statement checks if the search value equals the target value. If a match is found, the target value exists in the list, and it prints True to indicate that a match has been found. The loop is exited immediately using the break statement.

If no match is found after checking all the elements in the list, the loop completes without encountering a break statement, and the code following the loop continues execution without printing anything.

So, with these 3 examples, we have demonstrated how to search for a match in a list in Python. I do hope you found this tutorial helpful!

Video, Further Resources & Summary

Do you need more explanations on how to detect a match in a list in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

In the video, we explain in some more detail how to find a match in a list in Python.


The YouTube video will be added soon.


Furthermore, I encourage you to check out other interesting Python list tutorials on Statistics Globe, starting with these ones:

This post has shown how to find a match in a list in Python. In case you have further questions, you may leave a comment below.


R & Python Expert Ifeanyi Idiaye

This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.


Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.