Python – Check for Sublist in List

Sometimes, while working with Python Lists, we can have a problem in which we need to check if a particular sublist is contained in 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 above functions can be used to solve this problem. In this, we perform task of checking for sublist by incremental slicing using list slicing technique.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


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 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 desired length.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


Output :

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



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.