How To Check If All Elements In A Tuple Or List Are In Another?
For example, I want to check every elements in tuple (1, 2) are in tuple (1, 2, 3, 4, 5). I don't think use loop is a good way to do it, I think it could be done in one line.
Solution 1:
You can use set.issubset
or set.issuperset
to check if every element in one tuple or list is in other.
>>>tuple1 = (1, 2)>>>tuple2 = (1, 2, 3, 4, 5)>>>set(tuple1).issubset(tuple2)
True
>>>set(tuple2).issuperset(tuple1)
True
Solution 2:
I think you want this: ( Use all )
>>> all(i in (1,2,3,4,5) for i in (1,2))
True
Post a Comment for "How To Check If All Elements In A Tuple Or List Are In Another?"