Change language

Python | Check if the list is in another list

| |

Examples :

  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

Simple naive approach — use two for loops and check if the whole list A is contained in list B or not. If such a position occurs in list A, then break the loop and return true, otherwise false

# Python3 list checker
# contained in another list without breaking order

 

def removeElements (A, B):

for i in range ( len (B) - len (A) + 1 ):

for j in range ( len (A)):

if B [i + j]! = A [j]:

break

else :

return True

return False

  
# Driver code

A < / code> = [ 1 , 2 ]

B = [ 1 , 2 , 3 , 1 , 1 , 2 , 2 ]

print (removeElements (A, B))

Exit :

 True 

Approach # 2: Understanding the List

A more effective approach is to use the Comprehension List. First we initialize n with length A. Now we use a for loop to len (B) -n and check in each iteration if A == B [i: i + n] or not.

# Python3 program to remove elements
# list that repeats less than k times

 

def removeElements (A, B):

n = len (A)

return any (A = = B [i: i + n] for i in range ( len (B) - n + 1 ))

 

 
# Driver code

A = [ 1 , 2 ]

B = [ 1 , 2 , 3 , 1 , 1 , 2 , 2 ]

print (removeElements (A, B) )

Exit:

 True 

Approach # 3: Using the join module and map

Here we use the join , to concatenate both lists into strings and then use the in operator to check if list A is contained in B or not.

# Python3 program for removing elements
# a list that repeats less than k times

 < / p>

 

def removeElements (A , B):

return ’ , ’ . join ( map ( str , A)) in ’,’ . join ( map ( str , B))

  
# Driver code

A = [ ’x’ , ’ y’ , ’z’ ]

B = [ ’x’ , ’ a’ , ’ y’ , ’x’ , ’b’ , ’z’ ]

print (removeElements (A, B))

Exit:

 False 

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically