Shivkumar Shivkumar - 19 days ago 5
Python Question

Check if all elements of ListA exists in ListB using python

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

Answer

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