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.
When it is required to check if one tuple is a subset of the other, the 'issubset' method is used. The 'issubset' method returns True if all the elements of the set are present in another set, wherein the other set would be passed as an argument to the method.
One of the most important differences between list and tuple is that list is mutable, whereas a tuple is immutable. This means that lists can be changed, and tuples cannot be changed.
When it is required to check if two list of tuples are identical, the '==' operator is used. The '==' operator checks to see if two iterables are equal or not.
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
I think you want this: ( Use all )
>>> all(i in (1,2,3,4,5) for i in (1,2))
True
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With