Open In App

Python – Check for Sublist in List

Last Updated : 14 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Sometimes, while working with Python Lists, we can have a problem in which we need to check if a particular sublist is contained in an exact sequence in a list. This task can have application in many domains such as school programming and web development. Let’s discuss certain ways in which this task can be performed.
 

Input : test_list = [5, 6, 3, 8, 2, 1, 7, 1], sublist = [8, 2, 3] 
Output : False


Input : test_list = [5, 6, 3, 8, 2, 3, 7, 1], sublist = [8, 2, 3] 
Output : True 


Method #1 : Using loop + list slicing 
The combination of the above functions can be used to solve this problem. In this, we perform the task of checking for sublist by incremental slicing using the list slicing technique.
 

Python3
# Python3 code to demonstrate working of
# Check for Sublist in List
# Using loop + list slicing

# initializing list
test_list = [5, 6, 3, 8, 2, 1, 7, 1]

# printing original list
print("The original list : " + str(test_list))

# initializing sublist
sublist = [8, 2, 1]

# Check for Sublist in List
# Using loop + list slicing
res = False
for idx in range(len(test_list) - len(sublist) + 1):
    if test_list[idx: idx + len(sublist)] == sublist:
        res = True
        break

# printing result
print("Is sublist present in list ? : " + str(res))

Output
The original list : [5, 6, 3, 8, 2, 1, 7, 1]
Is sublist present in list ? : True

 
Method #2 : Using any() + list slicing + generator expression 
The combination of the above functions is used to solve this problem. In this, we perform the task of checking for any sublist equating to desired using any(), and list slicing is used to slice incremental list of the desired length.
 

Python3
# Python3 code to demonstrate working of
# Check for Sublist in List
# Using any() + list slicing + generator expression

# initializing list
test_list = [5, 6, 3, 8, 2, 1, 7, 1]

# printing original list
print("The original list : " + str(test_list))

# initializing sublist
sublist = [8, 2, 1]

# Check for Sublist in List
# Using any() + list slicing + generator expression
res = any(test_list[idx: idx + len(sublist)] == sublist
          for idx in range(len(test_list) - len(sublist) + 1))

# printing result
print("Is sublist present in list ? : " + str(res))

Output
The original list : [5, 6, 3, 8, 2, 1, 7, 1]
Is sublist present in list ? : True

Method #3 : Using in operator

In this, we will convert both the list to a string using join() method and map() method and then later use in operator to check for the sublist in list.

Python3
# Python3 code to demonstrate working of
# Check for Sublist in List
# Using in operator

# initializing list
test_list = [5, 6, 3, 8, 2, 1, 7, 1]

# printing original list
print("The original list : " + str(test_list))

# initializing sublist
sublist = [8, 2, 1]

# Check for Sublist in List
res = ''.join(map(str, sublist)) in ''.join(map(str, test_list))

# printing result
print("Is sublist present in list ? : " + str(res))

Output
The original list : [5, 6, 3, 8, 2, 1, 7, 1]
Is sublist present in list ? : True

Method #4: Using the in operator with zip() and *

Step-by-step algorithm:

  1. Create the main list of integers to search for the sublist.
  2. Create the sublist to search for in the main list.
  3. Use a list comprehension to create a list of all possible sublists of the main list with the same length as the sublist.
  4. Use the zip function to group the elements of each sublist created in step 3.
  5. Compare each group of elements created in step 4 to the elements of the sublist.
  6. If a group of elements matches the elements of the sublist, return True. Otherwise, return False.
Python3
#This is a Python code file to check whether a sublist is present in a list
#Initializing list
test_list = [5, 6, 3, 8, 2, 1, 7, 1]

#Initializing sublist to be checked
sublist = [8, 2, 1]

# printing original list
print("The original list : " + str(test_list))
#Using list comprehension and zip function to check for sublist
res = any(sublist == list(x) for x in zip(*[test_list[i:] for i in range(len(sublist))]))

#Printing the result
print("Is sublist present in list ? : " + str(res))
#This code is contributed by Vinay Pinjala.

Output
The original list : [5, 6, 3, 8, 2, 1, 7, 1]
Is sublist present in list ? : True

Time complexity: O(n*m), where n is the length of the main list and m is the length of the sublist.

Space complexity: O(n*m), where n is the length of the main list and m is the length of the sublist.

Method#5: Recursive Method (Not Efficient)

1. Define the recursive function `check_sublist_recursive` that takes in the original list (`test_list`) and the sublist (`sublist`) as arguments.
2. If the length of `sublist` is 0, return `True` since an empty list is a sublist of any list.
3. If the length of `test_list` is 0 and the length of `sublist` is not 0, return `False` since the `sublist` cannot be present in the `test_list`.
4. If the first element of `test_list` is equal to the first element of `sublist`, recursively call `check_sublist_recursive` with the first element of both lists removed.
5. If the first element of `test_list` is not equal to the first element of `sublist`, recursively call `check_sublist_recursive` with the first element of `test_list` removed.
6. Return the result of the recursive call.

Python3
def check_sublist_recursive(test_list, sublist):
    if len(sublist) == 0:
        return True
    if len(test_list) == 0:
        return False
    if test_list[0] == sublist[0]:
        return check_sublist_recursive(test_list[1:], sublist[1:])
    else:
        return check_sublist_recursive(test_list[1:], sublist)
test_list = [5, 6, 3, 8, 2, 1, 7, 1]
sublist = [8, 2, 1]
res = check_sublist_recursive(test_list, sublist)
print("Is sublist present in list ? : " + str(res))

Output
Is sublist present in list ? : True

Method to check if the list items are subset of another list (If we ignore the order, we can use set). This method does not solve the actual problem but solves a very related problem.

We can use the set intersection operation to check if all the elements of the sublist are present in the test_list. If the length of the intersection is equal to the length of the sublist, then we can conclude that the sublist is present in the test_list.

Python3
# Python3 code to demonstrate working of
# Check for Sublist in List

# initializing list
test_list = [5, 6, 3, 8, 2, 1, 7, 1]

# printing original list
print("The original list : " + str(test_list))

# initializing sublist
sublist = [8, 2, 7]

# Check for Sublist in List
if set(sublist).intersection(set(test_list)) == set(sublist):
    res = True
else:
    res = False

# printing result
print("Is sublist present in list ? : " + str(res))

Output
The original list : [5, 6, 3, 8, 2, 1, 7, 1]
Is sublist present in list ? : True

Time Complexity: O(n), where n is the length of the test_list, since we need to convert both the test_list and the sublist to sets and perform an intersection operation.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads