Given two lists A and B, write a Python program to Check if list A is contained in list B without breaking A’s order.
Input : A = [1, 2], B = [1, 2, 3, 1, 1, 2, 2] Output : True Input : A = ['x', 'y', 'z'], B = ['x', 'a', 'y', 'x', 'b', 'z'] Output : False
Approach #1 : Naive Approach
A simple naive approach is to use two for loops and check if the whole list A is contained within list B or not. If such a position is met in list A, then break the loop and return true, otherwise false
Approach #2 : List comprehension
A more efficient approach is to use List comprehension. We first initialize ‘n’ with length of A. Now use a for loop till len(B)-n and check in each iteration if
A == B[i:i+n] or not.
Approach #3 : Using join and map module
Here we use join to join both lists to strings and then use in operator to check if list A is contained in B or not.
- Python | Check if a nested list is a subset of another nested list
- Python | Insert list in another list
- Python | Update a list of tuples using another list
- Python | Move one list element to another list
- Python - Group Sublists by another List
- Python - Sum of each List element occurrence in another
- Check if a string can be repeated to make another string
- Python | Remove sublists that are present in another sublist
- Python | Add one string to another
- Python - Raise elements of tuple as power to another tuple
- Python | Test if string is subset of another
- Python | Check if a list exists in given list of lists
- Python - Check List elements from Dictionary List
- Python | Convert list of string to list of list
- Python | Convert list of tuples to list of list
- Python | Convert List of String List to String List
- Python program to check if the list contains three consecutive common numbers in Python
- Python | Check if all the values in a list that are greater than a given value
- Python - Check if a list is empty or not
- Python List Equality | Program to check if two given matrices are identical
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.
Improved By : ManasChhabra2