How I Check If a List Contains a Sublist in Python

Recently, I was working on a data-cleaning project where I had to verify if a smaller list of values existed inside a larger dataset.

At first, I thought Python might have a direct built-in method for this. But just like in Excel, where you don’t always get a direct option (for example, filtering strikethrough text), in Python too, you often need a workaround.

Over the years, I’ve tried different approaches, from simple loops to using built-in functions in Python. In this tutorial, I’ll show you the exact methods I use to check whether a list contains a sublist in Python.

Method 1 – Use a Loop in Python

The easiest way is to manually check if each slice of the main list matches the sublist.

Here’s how I do it:

def contains_sublist(main_list, sub_list):
    n, m = len(main_list), len(sub_list)
    for i in range(n - m + 1):
        if main_list[i:i+m] == sub_list:
            return True
    return False

# Example
main = [10, 20, 30, 40, 50, 60]
sub = [30, 40, 50]

print(contains_sublist(main, sub))  # Output: True

You can refer to the screenshot below to see the output.

C:\Users\GradyArchie\Downloads\images\python find sublist in list.jpg

In this example, [30, 40, 50] exists inside the main list, so the function returns True. This method is easy to understand and works well when you’re starting.

Method 2 – Use Python’s any() Function

If you’re like me and prefer writing cleaner, one-liner code, any() is a great choice.

def contains_sublist(main_list, sub_list):
    return any(main_list[i:i+len(sub_list)] == sub_list 
               for i in range(len(main_list) - len(sub_list) + 1))

# Example
main = [5, 10, 15, 20, 25]
sub = [15, 20]

print(contains_sublist(main, sub))  # Output: True

You can refer to the screenshot below to see the output.

python list contains sublist

Here, any() goes through each possible slice of the main list and checks if it matches the sublist. It’s concise, efficient, and effective.

Method 3 – Use str() Conversion in Python

Sometimes I use a quick trick: convert both lists to strings and check if one is in the other.

def contains_sublist(main_list, sub_list):
    return str(sub_list)[1:-1] in str(main_list)[1:-1]

# Example
main = ["NY", "CA", "TX", "FL", "WA"]
sub = ["CA", "TX"]

print(contains_sublist(main, sub))  # Output: True

You can refer to the screenshot below to see the output.

python check if sublist in list

But be careful: this method can give false positives if values overlap in unexpected ways. I only use it for simple, controlled datasets.

Read Use Exponential Functions in Python

Method 4 – Use collections.deque for Large Lists

When I worked on a financial dataset with millions of rows, performance became an issue. In such cases, I use collections.deque to optimize sliding window checks.

from collections import deque

def contains_sublist(main_list, sub_list):
    m = len(sub_list)
    window = deque(main_list[:m], maxlen=m)

    if list(window) == sub_list:
        return True

    for elem in main_list[m:]:
        window.append(elem)
        if list(window) == sub_list:
            return True
    return False

# Example
main = list(range(1, 1000000)) + [999999, 1000000]
sub = [999998, 999999, 1000000]

print(contains_sublist(main, sub))  # Output: True

You can refer to the screenshot below to see the output.

This approach is memory-efficient and works well for very large datasets.

Method 5 – Use numpy (For Numeric Lists)

If you’re dealing with numeric data and already using NumPy, you can take advantage of its array operations.

import numpy as np

def contains_sublist(main_list, sub_list):
    main_arr = np.array(main_list)
    sub_arr = np.array(sub_list)
    for i in range(len(main_arr) - len(sub_arr) + 1):
        if np.array_equal(main_arr[i:i+len(sub_arr)], sub_arr):
            return True
    return False

# Example
main = [100, 200, 300, 400, 500]
sub = [300, 400]

print(contains_sublist(main, sub))  # Output: True

This is especially useful in data science projects where NumPy is already part of the workflow.

Practical Example – Check State Codes

Let’s take a practical example from the USA. Suppose you’re analyzing sales data and want to check if a sequence of state codes appears in your dataset.

states = ["NY", "NJ", "PA", "OH", "MI", "IL", "WI", "MN"]
target = ["OH", "MI", "IL"]

print(contains_sublist(states, target))  # Output: True

This is a common scenario when you need to validate ordered sequences in business datasets.

While Python doesn’t have a direct built-in function to check if a list contains a sublist, we have multiple ways to achieve it.

For small datasets, a simple loop or any() works perfectly. For larger datasets, methods like deque or numpy can give better performance.

I often switch between these methods depending on the project. The key is to pick the one that keeps your code both efficient and easy to read.

You may like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.