Sublist Presence in a List

Write a Python function that takes two lists as input – a main list and a sublist. The function should return True if the sublist is present in the main list, and False otherwise.

Example 1:

Input: [1, 2, 3, 4, 5], [3, 4] 
Output: True

Example 2:

Input: ['a', 'b', 'c', 'd', 'e'], ['c', 'e'] 
Output: False

Consider using list slicing.

def is_sublist(main_list, sublist):
    sub_length = len(sublist)
    for i in range(len(main_list)):
        if main_list[i:i+sub_length] == sublist:
            return True
    return False

© Let’s Data Science


Unlock AI & Data Science treasures. Log in!