Python: Check whether a list contains a sublist
Python List: Exercise - 32 with Solution
Check if List Contains Sublist
Write a Python program to check whether a list contains a sublist.
Sample Solution:
Python Code:
# Define a function named 'is_Sublist' that checks if list 's' is a sublist of list 'l'
def is_Sublist(l, s):
sub_set = False # Initialize a flag 'sub_set' to indicate whether 's' is a sublist of 'l
# Check if 's' is an empty list; in this case, 's' is a sublist of any list
if s == []:
sub_set = True
# Check if 's' is equal to 'l'; if so, 's' is a sublist of 'l
elif s == l:
sub_set = True
# Check if the length of 's' is greater than the length of 'l'; 's' cannot be a sublist in this case
elif len(s) > len(l):
sub_set = False
else:
# Iterate through the elements of 'l'
for i in range(len(l)):
# Check if the current element in 'l' matches the first element in 's'
if l[i] == s[0]:
n = 1
while (n < len(s)) and (l[i + n] == s[n]):
n += 1
# If 'n' equals the length of 's', 's' is a sublist of 'l
if n == len(s):
sub_set = True
# Return the value of 'sub_set,' which indicates whether 's' is a sublist of 'l
return sub_set
# Define list 'a,' 'b,' and 'c'
a = [2, 4, 3, 5, 7]
b = [4, 3]
c = [3, 7]
# Check if 'b' is a sublist of 'a' and print the result
print(is_Sublist(a, b))
# Check if 'c' is a sublist of 'a' and print the result
print(is_Sublist(a, c))
Sample Output:
True False
Flowchart:
Python Code Editor:
Previous: Write a Python program to count the number of elements in a list within a specified range.
Next: Write a Python program to generate all sublists of a list.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/python-exercises/list/python-data-type-list-exercise-32.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics