27. Problem to check whether a list contains a sublist or not.

In this program, we will take two lists as inputs and check whether the list contains a sublist with the help of the below-given steps.

List contains a sublist:

Steps to solve the program
  1. Take two lists as input.
  2. Check whether list2 is a sublist of list1 (i.e. all the elements from the list2 are in list1.
  3. Use for loop for this purpose.
  4. Print the output.
				
					#Input list
list1 = [22,45,67,12,78]

#Sublist
list2 = [45,12]

#Creating count variable
count = 0

for value in list1:
    for element in list2:
        if value == element:
            count += 1
            
#Printing output
if count == len(list2):
    print("Sublist exists")
else:
    print("Sublist does not exists")
				
			

Output :

				
					Sublist exists
				
			

Related Articles

Find the max, min, and sum of the list

Generate all sublists with 5 or more elements

Leave a Comment