I have two lists
ListA = [1,9,6,3,2,4]
ListB = range(min(ListA),(max(ListA)+1))
i.e ListB = [1,2,3,4,5,6,7,8,9]
I want to check if all elements of ListA exists in ListB
I have two lists
ListA = [1,9,6,3,2,4]
ListB = range(min(ListA),(max(ListA)+1))
i.e ListB = [1,2,3,4,5,6,7,8,9]
I want to check if all elements of ListA exists in ListB
Use issubset
to achieve that (I prefer to rename your variables to make them more pythonic):
l1 = [1, 9, 6, 3, 2, 4]
l2 = [1, 2, 3, 4, 5, 6, 7, 8, 9]
Output:
>>> set(l1).issubset(set(l2))
True
You may also use issuperset
as follows:
>>> set(l2).issuperset(set(l1))
True
You may use set()
to check ListA
is subset of ListB
as:
>>> ListA = [1, 9, 6, 3, 2, 4]
>>> ListB = [1,2,3,4,5,6,7,8,9]
>>> set(ListA) <= set(ListB) # OR, using `set(ListA).issubset(set(ListB))`
True
OR, you may use all()
to check that iteratively:
# Iterates over ListA and checks each item is in ListB
>>> all(item in ListB for item in ListA)
True