Check Sublist in List

Tags: List, Sublist

Given a list of integers and a target sublist, write a Python program to check if the target sublist is contained within the list.

Example 1:

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

Example 2:

Input: [10,20,30,40,50], [60,70] Output: False

You can convert the list and sublist to strings and check if the sublist string is a substring of the list string.

def is_sublist(nums, sublist):
    return ' '.join(map(str, sublist)) in ' '.join(map(str, nums))

© Let’s Data Science


Unlock AI & Data Science treasures. Log in!