ContainsOnly
✖
ContainsOnly
is an operator form that yields True when the object to which it is applied contains only elements that appear in list2.
Details and Options

- In ContainsOnly[list1,list2], the listi must be lists of expressions, or representations of them, namely objects with heads like SparseArray, QuantityArray, Association or EntityClass.
- ContainsOnly[list1,list2,SameTesttest] applies test to each pair of elements in the listi to determine whether they should be considered the same.
- ContainsOnly[list2][list1] is equivalent to ContainsOnly[list1,list2].
Examples
open allclose allBasic Examples (2)Summary of the most common use cases
Scope (4)Survey of the scope of standard use cases
This is one of the longest English words containing only vowels:

https://wolfram.com/xid/0btm8ukzwd8g-g1kme4

Use ContainsOnly in operator form:

https://wolfram.com/xid/0btm8ukzwd8g-yhn4zc


https://wolfram.com/xid/0btm8ukzwd8g-vkve4c


https://wolfram.com/xid/0btm8ukzwd8g-6eowz2

The number of songs composed either by John Lennon or Paul McCartney, or by both together:

https://wolfram.com/xid/0btm8ukzwd8g-9cqisv

ContainsOnly works with associations:

https://wolfram.com/xid/0btm8ukzwd8g-jtfkx9


https://wolfram.com/xid/0btm8ukzwd8g-oyyckd

Options (2)Common values & functionality for each option
SameTest (2)
By default, ContainsOnly considers elements to be the same if they are identical:

https://wolfram.com/xid/0btm8ukzwd8g-va3try


https://wolfram.com/xid/0btm8ukzwd8g-m67or7

Changes of units are taken into account, as long as they represent the exact same quantity:

https://wolfram.com/xid/0btm8ukzwd8g-jtl0vb

Numerical approximations are not considered as the same object:

https://wolfram.com/xid/0btm8ukzwd8g-t34h5h

Use Equal as the comparison function to have numerical tolerance:

https://wolfram.com/xid/0btm8ukzwd8g-gz6vvk

String comparisons are case sensitive by default:

https://wolfram.com/xid/0btm8ukzwd8g-1pybm9

Convert to lower case before comparing:

https://wolfram.com/xid/0btm8ukzwd8g-3b2q37

Applications (1)Sample problems that can be solved with this function
Properties & Relations (3)Properties of the function, and connections to other functions
Reversing the arguments of ContainsOnly is equivalent to using ContainsAll:

https://wolfram.com/xid/0btm8ukzwd8g-opnt8r

https://wolfram.com/xid/0btm8ukzwd8g-py8oi3

https://wolfram.com/xid/0btm8ukzwd8g-ehhrdi

ContainsOnly[list1,list2] is equivalent to SubsetQ[list2,list1]:

https://wolfram.com/xid/0btm8ukzwd8g-3jdwr

https://wolfram.com/xid/0btm8ukzwd8g-xyoj5b

https://wolfram.com/xid/0btm8ukzwd8g-2gp42t

ContainsOnly[{},list] always returns True:

https://wolfram.com/xid/0btm8ukzwd8g-2qq8ei


https://wolfram.com/xid/0btm8ukzwd8g-uq27qr

Wolfram Research (2015), ContainsOnly, Wolfram Language function, https://reference.wolfram.com/language/ref/ContainsOnly.html.
Text
Wolfram Research (2015), ContainsOnly, Wolfram Language function, https://reference.wolfram.com/language/ref/ContainsOnly.html.
Wolfram Research (2015), ContainsOnly, Wolfram Language function, https://reference.wolfram.com/language/ref/ContainsOnly.html.
CMS
Wolfram Language. 2015. "ContainsOnly." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/ContainsOnly.html.
Wolfram Language. 2015. "ContainsOnly." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/ContainsOnly.html.
APA
Wolfram Language. (2015). ContainsOnly. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/ContainsOnly.html
Wolfram Language. (2015). ContainsOnly. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/ContainsOnly.html
BibTeX
@misc{reference.wolfram_2025_containsonly, author="Wolfram Research", title="{ContainsOnly}", year="2015", howpublished="\url{https://reference.wolfram.com/language/ref/ContainsOnly.html}", note=[Accessed: 20-June-2025
]}
BibLaTeX
@online{reference.wolfram_2025_containsonly, organization={Wolfram Research}, title={ContainsOnly}, year={2015}, url={https://reference.wolfram.com/language/ref/ContainsOnly.html}, note=[Accessed: 20-June-2025
]}