0
How to check that tuple a contains all the elements of tuple B
7 Answers
+ 1
You do not specify clearly what "all elements" means.
Does it include duplicates?
If not, then Jan Markus has the right solution, if it does include duplicates then this will do the trick.
print(all(i==j for i,j in zip(sorted(tA),sorted(tB))))
Edit: ^
forget this ^
Oma AKA Frogged
Has a more elegant solution
+ 2
print(all([True if i in tA else False for i in tB]))
The above will print True if tupleA(tA) contains all elements of tB else False.
+ 1
Louis nice!
+ 1
sorted(ta) ==sorted(tb)
i stood on the shoulders of Louis
his answer is best.
+ 1
What about the cases when TupleA has more elements than TupleB and still contains all the elements of TupleB?
tA = (2,4,6,8,4,4,12,7,9)
tB = (2,6,4,4,8)
The working solution:
print(all(tA.count(x)>=tB.count(x) for x in set(tB)))
https://code.sololearn.com/cKuGc94bWpiU/?ref=app
0
Can you write in code?
0
Vild you mean?