Find Sublist in List in Python (2 Examples)
Hi! This tutorial will show you how to detect a sublist in a list in the Python programming language.
Here is an overview:
Let’s get into it!
Create Example List
Here, we will create the example Python list of integers that we will use in the examples in this tutorial.
Therefore, in your Python programming IDE, run the code below to create the example list:
my_list = [1, 2, 3, 4, 5] print(my_list) # [1, 2, 3, 4, 5] print(type(my_list)) # <class 'list'>
With the example list created, let us now see two ways to perceive a sublist in the list.
The sublist we want to detect its existence in the list is defined below:
sublist = [2,3]
Let’s jump into the examples!
Example 1: Detect Sublist in List Using for Loop & Conditional if Statement
In this first example, we will use a for loop and the conditional if statement to detect the sublist in the list:
result = False for idx in range(len(my_list) - len(sublist) + 1): if my_list[idx: idx + len(sublist)] == sublist: result = True print(result) break # True
Here, we first initialize a boolean variable result as False.
Then, we iterate through the indices of my_list using the range() function, ensuring that sublist can fit starting from each index by subtracting the length of sublist from the length of my_list and adding 1.
Within the loop, we check if the slice of my_list from the current index to the index plus the length of sublist is equal to sublist itself using the double equals operator denoted by (==).
If the condition is met, result is set to True, indicating that sublist has been found. The value of result is also printed at this point.
After finding sublist, the loop breaks using the break statement since there is no need to continue iterating.
If the loop completes without finding a match, result remains False.
Example 2: Detect Sublist in List Using any() Function
In this next example, we will use the any() function to perceive the sublist in the list:
result = any(my_list[idx: idx + len(sublist)] == sublist for idx in range(len(my_list) - len(sublist) + 1)) print(result) # True
In the above example, we use a generator expression with the any() function to iterate over the indices of the main list.
For each index, we check if the slice of my_list from that index to the index plus the length of sublist is equal to sublist itself.
The any() function returns True if at least one of the comparisons evaluates to True, and False otherwise.
The result of this evaluation is stored in the variable result.
Finally, the value of result is printed using the print() function, which indicates whether sublist was found (True) or not (False).
Video, Further Resources & Summary
Do you need more explanations on how to find a sublist 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 sublist 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:
- Find Union of Two Lists in Python (3 Examples)
- Are Dictionaries Faster than Lists in Python?
- Append to 2D List in Python (3 Examples)
- Transpose 2D List in Python (3 Examples)
- Using Lists in Python (Introduction)
- Introduction to Python Programming
This post has shown, using two examples, how to find a sublist in a list in Python. Your use case will determine which solution you would adopt.
I do hope you found this tutorial helpful! In case you have further questions, you may leave a comment below.
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.
Thank you!
Welcome to the Statistics Globe newsletter. From now on, I’ll send you regular emails about statistics, data science, AI, and programming with R and Python.
I’m Joachim Schork. On this website, I provide statistics tutorials as well as code in Python and R programming.
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.
Thank you!
Please check your email inbox and click the confirmation link to complete your subscription. If you don’t see the email within a few minutes, please also check your spam/junk folder.







